方式一: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 = ;//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 > && Response.IsClientConnected)
...{
int lengthRead = iStream.Read(buffer, , Convert.ToInt32(ChunkSize));//读取的大小
Response.OutputStream.Write(buffer, , 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, , 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();
}

.net 下载文件几种方式的更多相关文章

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

    测试时我以字符流的形式下载文件,可行,前几个仅作参考 protected void Button1_Click(object sender, EventArgs e)  {  /*  微软为Respo ...

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

    protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新的方法TransmitFile来解决使 ...

  3. Linux上删除大量文件几种方式对比

    目录 Linux上删除大量文件几种方式对比 1. rm删除:因为文件数量太多,rm无法删除(报错) 2. find查找删除:-exec 3. find查找删除:xargs 4. find调用-dele ...

  4. 加载映射文件几种方式和mapper接口注解执行sql语句

    一.加载映射文件几种方式 二.mapper接口注解执行sql语句 就将xml中的sql语句放到注解的括号中就可以,一般只用于简单的sql语句合适:

  5. svn插件下载的两种方式

     1.下载SVN插件     SVN插件下载地址及更新地址,你根据需要选择你需要的版本.现在最新是1.8.x  Links for 1.8.x Release:          Eclipse up ...

  6. JS下载文件常用的方式

    下载附件(image,doc,docx, excel,zip,pdf),应该是实际工作中经常遇到一个问题:这里使用过几种方式分享出来仅供参考; 初次写可能存在问题,有问题望指出 ​ 主要了解的几个知识 ...

  7. 使用隐藏form表单下载文件,解决url方式下载,由于环境问题而限制url长度,满足不了所有的需求!

    一 对于某些环境导出是直接用wiondow.href=url直接导出下载,有些业务需求,如员工档案等字段比较多的时候,全选导出就会引发异常,由于Nginx转发长度限制的问题, 如果运维不愿意改变环境, ...

  8. [笔记]Go语言写文件几种方式性能对比

    Go语言中写文件有多种方式,这里进行如下几种方式的速度对比: 打开文件,写入内容,关闭文件.如此重复多次 打开文件,写入内容,defer 关闭文件.如此重复多次 打开文件,重复多次写入内容,defer ...

  9. Asp.Net 之 下载文件的常用方式

    1.直接使用Response.TransmitFile(filename)方法 protected void Button_Click(object sender, EventArgs e) { /* ...

随机推荐

  1. a new blog from MarsEdit

    终于在网上找到了一款mac 下的blog写作和发布工具了. 先测试一下.

  2. git学习:多人协作,标签管理

    多人协作: 查看远程库的信息, git remote 推送分支到远程库 git push origin master/dev 注意:master是主分支,时刻需要与远程同步 dev是开发分支,也需要与 ...

  3. 使用Grunt 插件打包Electron Windows应用

    最近利用Electron来创建跨桌面应用的趋势似乎很火.看了几个用Electron开发的应用,这些应用在windows下面的安装方式,都是类似一个绿色软件的安装方法,下载.zip->解压到相应目 ...

  4. 如何利用OEM工具(ORACLE ENTERPRISE MANAGER),增加表空间

  5. App.Config详解及读写操作

    App.Config详解及读写操作   App.Config详解 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而 ...

  6. 图标:适配不同分辨 的 hdpi、mdpi、ldpi 文件夹

    一:不同的layout Android手机屏幕大小不一,有480×320, 640×360, 800×480.怎样才能让App自动适应不同的屏幕呢? 其实很简单,只需要在res目录下创建不同的layo ...

  7. PHP常用函数总结(不定期更新)

    array_merge函数和两个数组相加的区别 array_merge函数,在出现相同的索引数组,会把两个数组中的索引数组,进行融合,以第一个数组的索引最大值往后相加,如果是关联数组,后面数组的值覆盖 ...

  8. Windows Server 2012重复数据删除技术体验

    在企业环境中,对磁盘空间的需求是惊人的.数据备份.文件服务器.软件镜像.虚拟磁盘等都需要占据大量的空间.对此,微软在Windows Server 2012中引入了重复数据删除技术.重复数据删除技术通过 ...

  9. Coretext实现图文混排及Gif图片播放

    CoreText是iOS3.2推出的一套文字排版和渲染框架,可以实现图文混排,富文本显示等效果. CoreText中的几个重要的概念:  CTFont CTFontCollection CTFontD ...

  10. java remote debug parameters

    java -agentlib:jdwp=transport=dt_socket,server=y,address=8000,suspend=n