C#从服务器下载文件可以使用下面4个方法:TransmitFile、WriteFile、WriteFile和流方式下载文件,并保存为相应类型,方法如下:

、TransmitFile实现下载
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/z.zip");
Response.TransmitFile(filename);
}
、WriteFile实现下载
protected void Button2_Click(object sender, EventArgs e)
{
/*
using System.IO; */
string fileName = "asd.txt";//客户端保存的文件名
string filePath = Server.MapPath("DownLoad/aaa.txt");//路径 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.txt";//客户端保存的文件名
string filePath = Server.MapPath("DownLoad/aaa.txt");//路径 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 = "aaa.txt";//客户端保存的文件名
string filePath = Server.MapPath("DownLoad/aaa.txt");//路径 //以字符流的形式下载文件
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();
}

C#根据流下载文件的更多相关文章

  1. js文件流下载通用方法

    通常我们会用到文件流下载文件,下面给大家一个通用的文件流下载的js /* *下载文件 * options:{ * url:'', //下载地址 * isNewWinOpen:false,是否新窗口打开 ...

  2. Java下载文件(流的形式)

    @RequestMapping("download") @ResponseBody public void download(HttpServletResponse respons ...

  3. asp.net已流的方式下载文件

    string filePath = context.Server.MapPath("~/" + uploadFolder+"/"+file_name);//路径 ...

  4. 利用 Blob 处理 node 层返回的二进制文件流字符串并下载文件

    博客地址:https://ainyi.com/65 解释 | 背景 看到标题有点懵逼,哈哈,实际上是后端将文件处理成二进制流,返回到前端,前端处理这个二进制字符串,输出文件或下载 最近公司有个需求是用 ...

  5. Asp.Net 文件下载1——流下载(适用于大文件且防盗链)(转)

    使用流防盗链下载大文件 直接上 Asp.net 后置代码好了 using System; using System.Data; using System.Configuration; using Sy ...

  6. js实现使用文件流下载csv文件

    1. 理解Blob对象 在Blob对象出现之前,在javascript中一直没有比较好的方式处理二进制文件,自从有了Blob了,我们就可以使用它操作二进制数据了.现在我们开始来理解下Bolb对象及它的 ...

  7. koa2基于stream(流)进行文件上传和下载

    阅读目录 一:上传文件(包括单个文件或多个文件上传) 二:下载文件 回到顶部 一:上传文件(包括单个文件或多个文件上传) 在之前一篇文章,我们了解到nodejs中的流的概念,也了解到了使用流的优点,具 ...

  8. Java文件流下载并提示文件不存在

    做文件下载功能的时候,一般使用流的形式下载文件, 如果源文件不存在,下载页面可能就会没有提示,或者一片空白 用户操作之后可能一头雾水,那如何友好提示呢? 想到的有两种 1.可以尝试下载一个名称为:文件 ...

  9. fetch的文件流下载及下载进度获取

    下载过程中,获取进度,fetch API并没有提供类似xhr和ajax的 progress所以用 getReader()来循环读取大小 let size = 0; fetch( URL() + `/s ...

随机推荐

  1. angular6 表单验证

    这里使用的是模型驱动的表单 1.app.module.ts import { ReactiveFormsModule } from '@angular/forms'; @NgModule({ ... ...

  2. 9. [mmc subsystem] host(第三章)——sdhci-pltfm说明

    一.sdhci-pltfm说明 sdhci-pltfm并不是实际某个host的driver. sdhci-pltfm是指在sdhci core的基础上,提供了统一对sdhci_host的必要属性进行解 ...

  3. JMETER 计数器的表现

    1.计数器在线程中表现(虚拟用户). 线程组使用4个线程即4个虚拟用户. 如果不勾选每用户独立跟踪计数器. 那么这个计数器将会累加4次. 计算结果为7. 勾选的情况. 这里我们可以看到 index 一 ...

  4. 两道DP,四年修一次路

    第十一届:山区修路 题目描述 SNJ位于HB省西部一片群峰耸立的高大山地,横亘于A江.B水之间,方圆数千平方公里,相传上古的神医在此搭架上山采药而得名.景区山峰均在海拔3000米以上,堪称" ...

  5. 201971010131-张兴盼《面向对象程序设计(java)》第十二周学习总结

      内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p/ ...

  6. 201871010135 张玉晶《面向对象程序设计(java)》第6-7周学习总结

    201871010135 张玉晶<面向对象程序设计(java)>第6-7周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...

  7. JVM-卡表(Card Table)

    简介 现代JVM,堆空间通常被划分为新生代和老年代.由于新生代的垃圾收集通常很频繁,如果老年代对象引用了新生代的对象,那么,需要跟踪从老年代到新生代的所有引用,从而避免每次YGC时扫描整个老年代,减少 ...

  8. Django之创建项目、目录层级、基本操作

    创建项目 在合适的位置创建一个目录 打开cmd窗口,进入到创建的目录内 输入django-admin startproject project 目录层级 manage.py 是一个命令行工具,可以使我 ...

  9. maxima已知方程,计算结果

  10. zz1998_Efficient Backprop笔记

    1998_Efficient Backprop笔记 A few practical tricks 1. Stochastic vs Batch learning 在最小值附近震荡的幅度与学习速率成比例 ...