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");//路径//以字符流的形式下载文件 需要使用FileAccess.Read,不然第二次会锁定文件无法打开FileStream fs = new FileStream(filePath, FileMode.Open,FileAccess.Read);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/FlyCat/archive/2012/07/04/2576156.html

ASP.NET文件下载相关推荐

  1. asp.net 文件下载的五种方式

    需求:下载站点目录下DownLoad文件夹中的logo.zip 前端HTML代码部分: <div>1.通过超链接下载:<a href="../DownLoadFiles/l ...

  2. asp.net 文件下载(txt,rar,pdf,word,excel,ppt)

    aspx 文件下载说起来一点都不难,但是在做的过程中还是遇到了一些小小的问题,就是因为这些小小的问题,导致解决起来实在是太难了,其中一个就是Response.End();导致下载文件出现线程终止的情况 ...

  3. asp.net文件下载,实现隐藏文件下载地址

    文件下载其实很简单.最简单的方式就是直接用a标签指向文件地址,但是这种方式暴露了文件的地址,不适合做积分下载.同时,如果文件为图片或者xml文件的话浏览器默认是打开文件而不是弹出下载窗口. 第二种就是 ...

  4. [转]asp.net文件下载方法...

    TransmitFile实现下载 protected void Button1_Click(object sender, EventArgs e) {          /*           微软 ...

  5. ASP.NET 文件下载 .

    <a href="download.ashx?url=<%=Server.UrlEncode("111.txt")%>">下载</ ...

  6. ASP.NET 文件下载

    下载页面:  <a href="download.ashx?url=<%=Server.UrlEncode("说明.txt")%>">下 ...

  7. c#.Net MVC服务端下载文件、限制下载速度、大文件下载、对性能的影响、对大文件的支持、对断点续传和多线程下载的支持

    一.ASP.NET文件下载各种方式比较:对性能的影响.对大文件的支持.对断点续传和多线程下载的支持 asp.net里提供了多种方式,从服务器端向客户端写文件流,实现客户端下载文件.这种技术在做防下载系 ...

  8. 任务41:Individual authentication 模板

    任务41:Individual authentication 模板 使用命令行创建项目 会自动加上ef core和Identity core代码的示例 默认创建mvc是不带Identity的 dotn ...

  9. 任务42:EF Core Migration

    任务42:EF Core Migration 任务42:EF Core Migration 右边的是在VS2017中使用的命令,左边是在VSCode 的DOS窗体中使用的 最新版本的core 2.2. ...

最新文章

  1. 前端基础---HTML
  2. pythonhistogram教程_OpenCV-Python 直方图-4:直方图反投影 | 二十九
  3. 【C 语言】指针 与 数组 ( 指针 | 数组 | 指针运算 | 数组访问方式 | 字符串 | 指针数组 | 数组指针 | 多维数组 | 多维指针 | 数组参数 | 函数指针 | 复杂指针解读)
  4. 应用程序池超出其作业限制设置_网站改版注意事项 - 蜘蛛池
  5. ITK:在一张图像中设置像素值
  6. PyTorch Tutorial
  7. 用Heartbeat实现web服务器高可用
  8. mantis apache mysql_Debian下mysql5+php5+apache2+mantis服务器升级
  9. Omnibus test
  10. Datalogic得利捷携最新技术与设备亮相2019两大工业展
  11. K-means算法实现及分析
  12. 为什么总是封板又打开涨停_股票反复打开涨停是什么原因?
  13. 计算机基础技能应用查询中心,《计算机基础及技能训练》大纲
  14. 短视频直播平台第三方特效SDK接入教程(完整版)
  15. 苹果最新发布的16 英寸 MacBook Pro有哪些亮点和不足之处?
  16. 计算机系统英语参考文献短,计算机操作系统专著参考文献 计算机操作系统英语参考文献哪里找...
  17. CNN结构演变总结(二)轻量化模型
  18. MessageFormatter
  19. FMDB iphone 数据库
  20. 霍营到北土城时间记录

热门文章

  1. HDU_oj_2047 阿牛的EOF牛肉面
  2. spring boot+mybatis整合
  3. spring+mybatis之声明式事务管理初识(小实例)
  4. daterangepicker 日期范围插件自定义 可选 年份
  5. NSArray ----NSMutableArray
  6. CSS实现背景透明而背景上的文字图片不透明
  7. jquery常用选择器
  8. ACM PKU1703 Find them, Catch them
  9. Android Fragments 详细使用
  10. bzoj4316: 小C的独立集