WriteFile 方式:

 1         protected void BtnDownload_Click(object sender, EventArgs e)
 2         {
 3             //客户端保存的文件名
 4             string fileName =  "a.xls";
 5 
 6             //服务器文件路径
 7             string filePath = Server.MapPath("~/a.xls");
 8 
 9             if (System.IO.File.Exists(filePath))
10             {
11                 //存在文件
12                 FileInfo fileInfo = new FileInfo(filePath);
13                 Response.Clear();
14                 Response.ClearContent();
15                 Response.ClearHeaders();
16                 Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
17                 Response.AddHeader("Content-Length", fileInfo.Length.ToString());
18                 Response.AddHeader("Content-Transfer-Encoding", "binary");
19                 Response.ContentType = "application/octet-stream";
20                 Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
21                 Response.WriteFile(fileInfo.FullName);
22                 Response.Flush();
23                 Response.End();
24             }
25             else
26             {
27                 Response.Write("<script>alert(\'文件不存在!请上传\')</script>");
28             }
29         }

 

版权声明:本文为codingmonkey原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/codingmonkey/p/5919934.html