本文转载自:http://www.cnblogs.com/weixing/archive/2012/02/27/2369567.html

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=z.zip");
  string filename = Server.MapPath("DownLoad/aaa.zip");
  Response.TransmitFile(filename);
  }

//WriteFile实现下载
  protected void Button2_Click(object sender, EventArgs e)
  {
  /*
  using System.IO;
   
  */

string fileName ="aaa.zip";//客户端保存的文件名
  string filePath=Server.MapPath("DownLoad/aaa.zip");//路径

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 = "aaa.zip";//客户端保存的文件名
  string filePath = Server.MapPath("DownLoad/aaa.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 = "aaa.zip";//客户端保存的文件名
  string filePath = Server.MapPath("DownLoad/aaa.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/itzhanghb/articles/4040763.html

asp.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. .net 下载文件几种方式

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

  4. asp.net下载的三种方式

    Code  protected void Button1_Click(object sender, EventArgs e)      {         /*           微软为Respon ...

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

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

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

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

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

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

  8. python 打开文件4种方式

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

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

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

最新文章

  1. javascript运动系列第九篇——碰撞运动
  2. ThinkPHP 中M方法和D方法的具体区别
  3. 数据分析之pandas常见的数据处理(四)
  4. dd linux 格式化u盘启动盘_linux dd命令[用于制作U盘启动盘的神奇的命令]
  5. BroadLeaf项目搜索功能改进
  6. 智能运维(AIOps)时代开启,一文帮你快速了解其定义与发展现状
  7. Futter基础第12篇: 实现TabController定义顶部tab切换,并介绍生命周期函数
  8. 在 lamp(centos)下配置二级 域名 、虚拟主机
  9. android源码编译完成之后
  10. linux自定义服务
  11. linux查询电信端口,linux下获取电信,网通,铁通IP地址
  12. Android开发高级进阶之Android开发艺术探索笔记重要知识点
  13. python编写游戏测试机器人客户端(四)
  14. 全国社会媒体处理大会即将召开,一文详解四天议程精华
  15. lis25ba_实验LIS25BA骨振动传感器采集音频
  16. iar升级芯片库_STM8 IAP升级程序设计详解 - IAR环境
  17. 离职后心生不满,西安某医院运维“炫技性报复”破坏诊疗系统,被依法刑拘...
  18. TDC综述(四)——参考文献
  19. 计算机硬盘内存不足,电脑磁盘不够怎么办_电脑磁盘空间不足的解决教程
  20. Vue - 获取汉字拼音首字母(将汉字转为拼音,取出首字母)

热门文章

  1. [leetcode]Palindrome Number @ Python
  2. php下memcache结合数据库
  3. Oracle 11G 64位发布出现错误
  4. 【转】通过IRQL看NT内核
  5. Etherchannel 备忘
  6. WinXP无需激活的秘技
  7. 中油C语言第一次在线作业,中石油华东《程序设计(C语言)》2020年春季学期在线作业(二)...
  8. mysql8.0windows,Windows下mysql 8.0.12 安装详细教程
  9. 数据科学入门与实战:Seaborn002热力图等
  10. android studio连接服务端代码,Android studio - 无法连接到LDAP服务器(示例代码)