//客户端保存的文件名
string fileName = "222.xls";
//被下载文件的服务器绝对路径
string filePath = Server.MapPath("file/" + doc.FileName);
Response.Clear(); //清空缓存
//以字符流的形式下载文件
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length]; //获取文件字节长度
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
//下载完成后删除文件
File.Delete(Server.MapPath("file/" + doc.FileName));
Response.End();
C# 流方式下载文件
最新推荐文章于 2024-04-19 10:39:57 发布