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/sjqq/p/6497880.html

asp.net C#实现下载文件的六种方法实例相关推荐

  1. ASP.NET中上传下载文件

    ASP.NET中上传下载文件 //检查上传文件不为空 if(File1.PostedFile!=null) { stringnam=File1.PostedFile.FileName; //取得文件名 ...

  2. ASP.NET上传下载文件

    ASP.NET上传下载文件 上传文件: using System; using System.Collections.Generic; using System.Linq; using System. ...

  3. php 当前页面下载文件,php实现当前页面点击下载文件的简单方法

    php实现当前页面点击下载文件的简单方法 发布于 2017-08-02 17:44:21 | 80 次阅读 | 评论: 0 | 来源: 网友投递 PHP开源脚本语言PHP(外文名: Hypertext ...

  4. php 网页内容下载,php实现当前页面点击下载文件的简单方法

    php控制器中代码 public function downFile($path = ''){ if(!$path) header("Location: /"); download ...

  5. Linux下scp无密码上传 下载 文件 目录的方法

    这篇文章主要介绍了Linux下scp无密码上传 下载 文件 目录的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下 在Linux下远程备份的时候,需要配置scp的 无密码复制文件.目录.就把这个 ...

  6. php通用下载方法,php实现当前页面点击下载文件的简单方法

    php控制器中代码 public function downFile($path = ''){ if(!$path) header("Location: /"); download ...

  7. 使用J2SE API读取Properties文件的六种方法(选择自 kindani 的 Blog )

    使用J2SE API读取Properties文件的六种方法 1.使用java.util.Properties类的load()方法 示例: InputStream in = lnew BufferedI ...

  8. php下载当前页面,php实现当前页面点击下载文件的简单方法

    php控制器中代码 public function downFile($path = ){ if(!$path) header(Location: /); download($path); } dow ...

  9. Microsoft Edge浏览器下载文件乱码修复方法(二)

    之前有写过"Microsoft Edge浏览器下载文件乱码修复方法",发现很多情况下下载文件乱码问题还是存在,这里对之前内容做简单补充,希望可以帮到大家. 方法二: 默认如果提示下 ...

  10. java读取propertiesshib_Java读取Properties文件的六种方法

    使用J2SE API读取Properties文件的六种方法 1.使用java.util.Properties类的load()方法 示例: InputStream in = lnew BufferedI ...

最新文章

  1. 《Adobe Fireworks CS6中文版经典教程》——1.2工具面板
  2. MYSQL中删除重复记录
  3. Android 国际化
  4. mysql能否在手机端运行_在手机上安装linux并部署mysql,jupyter用于数据探索
  5. java 图片分段上传_java 分段读取文件 并通过HTTP上传
  6. 【Python】利用pip下载Django超时失败的解决方法
  7. 拦截器手动添加spring注入方法
  8. 帝国cms 自动生成html,帝国cms二次开发用户访问自动生成html
  9. C语言银行账户管理系统
  10. 企业快速构建可落地的IT服务管理体系的五大关键点
  11. 有借必有贷,借贷必相等
  12. python多线程爬取妹子图
  13. 如何打开.pt文件?
  14. token 中有效期设置
  15. 【ACWing】909. 下棋游戏
  16. 牛客-小a与星际探索
  17. 浏览器无法打开百度首页,却可以打开其他网页解决方法
  18. DOM 树的解析渲染
  19. matlab emi滤波器设计,EMI滤波器设计.pdf
  20. 鼠标cursor属性和如何使用cursor的url属性

热门文章

  1. 计算机培训中学语文研修计划,初中语文个人研修计划书
  2. linux数据库定期备份,linux数据库定期备份
  3. sklearn 神经网络_DeepLearning学习(二):浅层神经网络
  4. 单片机红外模块知识分享,理论是日后实战的基础
  5. Hibernate的单向N-1关联(一)
  6. 【渝粤教育】国家开放大学2018年秋季 0242-21T机械制图 参考试题
  7. 区块链技术将有可能彻底颠覆音乐行业,思想启迪+P2Ptech,end
  8. 几个好用的makefile 几乎可以不用修改
  9. autotools工具介绍
  10. 通达信zig函数的python实现