方式一:TransmitFile实现下载。将指定的文件直接写入 HTTP 响应输出流,而不在内存中缓冲该文件。

protected void Button1_Click(object sender, EventArgs e)...{/**//*微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。代码如下:*/Response.ContentType = "application/x-zip-compressed";Response.AddHeader("Content-Disposition", "attachment;filename=CodeShark.zip");string filename = Server.MapPath("DownLoad/CodeShark.zip");Response.TransmitFile(filename);}

方式二:WriteFile实现下载,将指定的文件直接写入 HTTP 响应输出流。注意:对大型文件使用此方法时,调用此方法可能导致异常。可以使用此方法的文件大小取决于 Web 服务器的硬件配置。

protected void Button2_Click(object sender, EventArgs e)...{string fileName = "CodeShark.zip";//客户端保存的文件名string filePath = Server.MapPath("DownLoad/CodeShark.doc");//路径FileInfo fileInfo = new FileInfo(filePath);Response.Clear();Response.ClearContent();Response.ClearHeaders();Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);Response.AddHeader("Content-Length", fileInfo.Length.ToString());Response.AddHeader("Content-Transfer-Encoding", "binary");Response.ContentType = "application/octet-stream";Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");Response.WriteFile(fileInfo.FullName);Response.Flush();Response.End();}

方式三:WriteFile分块下载

 protected void Button3_Click(object sender, EventArgs e)...{string fileName = "CodeShark.zip";//客户端保存的文件名string filePath = Server.MapPath("DownLoad/CodeShark.zip");//路径System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);if (fileInfo.Exists == true)...{const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力byte[] buffer = new byte[ChunkSize];Response.Clear();System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);long dataLengthToRead = iStream.Length;//获取下载的文件总大小Response.ContentType = "application/octet-stream";Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));while (dataLengthToRead > 0 && Response.IsClientConnected)...{int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小Response.OutputStream.Write(buffer, 0, lengthRead);Response.Flush();dataLengthToRead = dataLengthToRead - lengthRead;}Response.Close();}}

方式四:流方式下载

 protected void Button4_Click(object sender, EventArgs e)...{string fileName = "CodeShark.zip";//客户端保存的文件名string filePath = Server.MapPath("DownLoad/CodeShark.zip");//路径//以字符流的形式下载文件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();Response.End();}

转载于:https://www.cnblogs.com/neru/p/4153483.html

.net 下载文件几种方式相关推荐

  1. javascript下载文件几种方式,接收后台返回流下载或直接下载文件

    目录 1 javascript下载文件7中方式 1.1 window.location.href下载 1.2 window.location下载 1.3 iframe下载 1.4 form表单的形式下 ...

  2. JS 下载文件两种方式总结

    后端返回Blob对象(文件流),完成导出.下载功能: 1.介绍Blob Blob Blob 对象表示一个不可变.原始数据的类文件对象.它的数据可以按文本或二进制的格式进行读取,也可以转换成 Reada ...

  3. asp.net下载文件几种方式

    本文转载自:http://www.cnblogs.com/weixing/archive/2012/02/27/2369567.html protected void Button1_Click(ob ...

  4. STM32_基础入门_程序下载的两种方式

    一.ISP串口下载 1.所涉及工具:MDK+FlyMcu 2.硬件连接 3.连接步骤 1.搜索并选择对应串口 2.选择要下载的hex文件,将"编程前重装文件"打勾 3.勾选&quo ...

  5. C++包含头文件几种方式

    该博文为原创文章,未经博主同意不得转载,如同意转载请注明博文出处 本文章博客地址:https://cplusplus.blog.csdn.net/article/details/105042381 包 ...

  6. python-网络图片下载(两种方式)

    利用python进行网络图片下载(两种方式) 1:方式一 1:代码 # coding=utf-8 """ @author: jiajiknag 程序功能: 图片批量下载_ ...

  7. python 打开文件4种方式

    python 打开文件4种方式 python 打开文件4种方式 [方式一]使用os.system()函数运行其他程序 [方式二]使用ShellExecute函数运行其他程序(用这种方法解决我的问题) ...

  8. linux上删除文件过慢,Linux上删除大量文件几种方式对比

    Linux上删除大量文件几种方式对比 linux上删除大量小文件删除测试:删除500000个小文件测试 生成文件: $ for i in $(seq 500000);do echo 'text' &g ...

  9. Linux之文件上传和下载(两种方式)

    一.rz上传.sz下载文件 rz是将Windows系统上的文件上传到Linux下sz是将Linux系统下的文件上传到Windows下 1.rz案例,输入rz下载文件到Linux下 [root@bogo ...

最新文章

  1. java 网络实验_Java实验五网络编程与安全
  2. sap 后台作业状态都是什么含义
  3. linux 权限 mask,Linux mask有效权限详解
  4. 4:如何实现对两个整数变量的值进行互换。
  5. 小程序服务器七牛云,基于七牛云 API 开发的微信小程序 SDK
  6. 光与夜之恋服务器维护中,光与夜之恋7月16日停服维护说明 维护详情一览
  7. MySQL 性能优化 分布式
  8. ef 批量保存 oracle,mybatis-oracle与mysql批量添加
  9. AIR访问操作系统剪贴板粘贴操作延迟呈现解决办法
  10. 移远EC600S-CN (3) - HTTP(S)应用GET请求POST请求,HTTP接入OneNET物联网云平台
  11. 系统分析员备考之系统工程篇(系统工程基础)
  12. flex 3 学习小结2
  13. QT学习笔记(十五):QLabel的点击事件(clicked)添加
  14. stm32h743单片机嵌入式学习笔记5-液晶屏汉字库原理
  15. Sketch 资源合集
  16. 随机梯度下降法 SGD
  17. 【vconsole】vconsole网页调试
  18. Binding的学习与使用
  19. 金融学经济学字母含义
  20. 大众点评的大数据实践转

热门文章

  1. linux能运行安卓模拟器吗,Ubuntu 14.04中使用模拟器运行Android系统
  2. JMS介绍与ActiveMQ安装
  3. django 设置媒体url_编写第一个 django 应用
  4. 54 计算机与信息科学类,计算机与信息科学类专业包括哪些
  5. 租房系统代码java_基于Java的租房管理系统的设计及实现.doc
  6. Dropping tests
  7. Choose and divide(唯一分解定理)
  8. I2C通信读写数据过程
  9. mysql数据库下的所有表字段
  10. screw ---- 数据库转文档