ASP.NET 后台下载文件方法

 1 void DownLoadFile(string fileName)

 2     {

 3         string filePath = Server.MapPath(fileName);//路径

 4 

 5         //以字符流的形式下载文件

 6         FileStream fs = new FileStream(filePath, FileMode.Open);

 7         byte[] bytes = new byte[(int)fs.Length];

 8         fs.Read(bytes, 0, bytes.Length);

 9         fs.Close();

10         Response.ContentType = "application/octet-stream";

11         //通知浏览器下载文件而不是打开

12         Response.AddHeader("Content-Disposition", "attachment; filename=" +     HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));

13         Response.BinaryWrite(bytes);

14         Response.Flush();

15         Response.End();

16     }

 

你可能感兴趣的:(asp.net)