原文地址为: asp.net 下载文件几种方式

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();

}

转载请注明本文地址: asp.net 下载文件几种方式

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

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

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

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

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

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

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

  4. .net 下载文件几种方式

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

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

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

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

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

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

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

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

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

  9. python 打开文件4种方式

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

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

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

最新文章

  1. 我非要捅穿这 Neutron(四)Open vSwitch in Neutron
  2. ic读卡器设置工具_IC设计工程师的职业前景真的有别人说的那么好吗?
  3. redis 能不能监听特定的key失效_php监听redis key失效触发回调事件
  4. python之协程函数、递归、二分法
  5. SAP HANA Backup and Recovery
  6. 程序员不会SQL有多难?高级工程师:可能工作都找不到!
  7. c语言中char buffer,C语言对char*的封装,形成buffer
  8. C++多态的练习——编写一个计算器项目
  9. wait(), notify(), notifyAll()等方法介绍
  10. wifi的主动扫描和被动扫描
  11. 大疆DJI Thermal SDK Linux编译
  12. Javaweb尚硅谷网上书城项目
  13. java restsharp_RestSharp 一个.NET(C#)的HTTP辅助类组件
  14. CS5211:DP/eDP to 2Port LVDS
  15. 虚拟服务器忘记密码,win7系统下VMware虚拟机忘记开机密码的解决方法
  16. stars-one的原创工具——星之小说下载器(JavaFx应用 )
  17. 明日方舟如何在电脑上玩 明日方舟模拟器教程
  18. Python3自然语言(NLTK)——语言大数据
  19. mysql之(1366,Incorrect string value:'\\xF0\\x9F\\x98\\x82...' for column 'content' at row 1)20
  20. 计算机学院条幅内容,学院迎新活动标语横幅

热门文章

  1. 解决ssh_exchange_identification:read connection reset by peer 原因
  2. rto净化效率计算公式_设备综合效率计算公式详解
  3. dubbo环境的简单搭建
  4. 一款炫丽的网页播放器插件
  5. react加水印_图片添加水印
  6. 给定一个设备编号区间[start, end],包含4或18的编号都不能使用,如:418、148、718不能使用,108可用
  7. git stash '储藏'当前工作状态
  8. Access violation at address 77106D4E in module 'ntdll.dll'. Write of address 004051A5.
  9. QT5写一个复数计算器(附源码)
  10. Android 远程协助(亲情助手||投屏||远程演示)实现思路和简单实现