public void DownloadFile(string path, string name){
try{
System.IO.FileInfo file = new System.IO.FileInfo(path);
Response.Clear();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一个不能被客户端读取的流,必须被下载
Response.ContentType = "application/ms-excel";
// 把文件流发送到客户端
Response.WriteFile(file.FullName);
// 停止页面的执行
//Response.End();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
catch (Exception ex){
Response.Write("<script>alert('系统出现以下错误://n" + ex.Message + "!//n请尽快与管理员联系.')</script>");
}
}
Response.Redirect ("nextpage.aspx", false);
catch (System.Threading.ThreadAbortException e){
throw;
}
接下来就可以通过其他函数或者事件调用这个函数来下载服务器上的文件了
protected void btnOutput_Click(object sender, EventArgs e){
try{
string strPath = Server.MapPath("/") + "Download//学生基本信息模版.xls";
DownloadFile(strPath, "学生基本信息模版.xls");
}
catch (Exception exp){
Response.Write("<script>alert('系统出现以下错误://n" + exp.Message + "!//n请尽快与管理员联系.')</script>");
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有