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

}

WriteFile实现下载的更多相关文章

  1. 转(Response.WriteFile 无法下载大文件解决方法)

    以前用Response.WriteFile(filename),但当遇到大文件时无法完整下载. 该方法最大的问题,它不是直接将数据抛到客户端,而是在服务器端(IIS)上缓存.当下载文件比较大时,服务器 ...

  2. .net中 登录 才能下载文件的方法 Response.WriteFile实现下载

    protected void Button2_Click(object sender, EventArgs e) { //可以在这里加是否登录的判断 string fileName = "c ...

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

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

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

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

  5. web开发下的各种下载方法

    利用TransmitFile方法,解决Response.BinaryWrite下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题. 代码如下: Response.Co ...

  6. C# 从服务器下载文件代码的几种方法

    一.//TransmitFile实现下载 protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一 ...

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

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

  8. .net中下载文件的方法(转)

    .net中下载文件的方法 一.//TransmitFile实现下载      protected void Button1_Click(object sender, EventArgs e)      ...

  9. Asp.Net 下载文件的几种方式

    asp.net下载文件几种方式 protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新的方法 ...

随机推荐

  1. onclik的使用.

    //好笨啊,这个居然忘记了,在行间家onclick事件要加();,addEventListener只要使用函数名字就好了 <!doctype html> <html> < ...

  2. Java设计模式-工厂方法模式(Factory Method)

    工厂方法模式(Factory Method) 工厂模式适合:凡是出现了大量的产品需要创建,并且具有共同的接口时,可以通过工厂方法模式进行创建.在以下的三种模式中,第一种如果传入的字符串有误,不能正确创 ...

  3. Java算法-各种题目总结

    1.排列计算 /*[程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析: 兔子 ...

  4. 图解Android - Android GUI 系统 (5) - Android的Event Input System

    Android的用户输入处理 Android的用户输入系统获取用户按键(或模拟按键)输入,分发给特定的模块(Framework或应用程序)进行处理,它涉及到以下一些模块: Input Reader: ...

  5. Hibernate中一级缓存和二级缓存使用详解

    一.一级缓存二级缓存的概念解释 (1)一级缓存就是Session级别的缓存,一个Session做了一个查询操作,它会把这个操作的结果放在一级缓存中,如果短时间内这个 session(一定要同一个ses ...

  6. jsp学习(五)

    在进行jsp与jdbc连接时,出现这样一个错误,提示如下: java.net.ConnectException: Connection refused: connect 后来发现是由于mysql数据库 ...

  7. jQuery的查找

    children([expr])概述 :取得一个包含匹配的元素集合中每一个元素的所有子元素的元素集合.可以通过可选的表达式来过滤所匹配的子元素.注意:parents()将查找所有祖辈元素,而child ...

  8. boost构造,解析json

    void asynDBCenter::isGetActorInfoEx(void* on_process, const char* arg) { std::stringstream ros(arg); ...

  9. (转)CSS3 @font-face

    @font-face是CSS3中的一个模块,他主要是把自己定义的Web字体嵌入到你的网页中,随着@font-face模块的出现,我们在Web的开发中使用字体不怕只能使用Web安全字体,你们当中或许有许 ...

  10. zabbix: failed to accept an incoming connection

    错误描述 查日志发现: failed to accept an incoming connection: connection from "192.168.186.132" rej ...