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

}

asp.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. asp传递参数的几种方式

    把下列代码分别加入a.asp和b.asp的<body></body>中,点提交,就可以将a.asp文本框的内容传给b.asp并显示出来 a.ASP <form actio ...

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

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

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

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

  6. C#实现下载的几种方式举例

    using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...

  7. js 前端实现文件流下载的几种方式

    后端是用Java写的一个下载的接口,返回的是文件流,需求:点击,请求,下载 利用iframe实现文件流下载 //把上一次创建的iframe删掉,不然随着下载次数的增多页面上会一堆的iframe var ...

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

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

  9. Quartus 11.0 的AS 下载方式和JTAG下载jic文件的方式

    FPGA下载的三种方式:主动配置方式(AS)和被动配置方式(PS)和最常用的(JTAG)配置方式: AS由FPGA器件引导配置操作过程,它控制着外部存储器和初始化过程,EPCS系列.如EPCS1,EP ...

随机推荐

  1. [多校2015.02.1006 高斯消元] hdu 5305 Friends

    题意: 给你n个人m条关系 每条关系包括a,b 代表a和b能够是线上朋友也能够是线下朋友 然后保证每一个人的线上朋友数和线下朋友数相等 问你有多少种组成方法 思路: 官方题解是爆搜+剪枝,然而并不会写 ...

  2. python3 http.client 网络请求

    python3 http.client 网络请求 一:get 请求 ''' Created on 2014年4月21日 @author: dev.keke@gmail.com ''' import h ...

  3. java记录linux top命令信息

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...

  4. localhost连接慢如何解决?

    localhost连接慢如何解决 php 环境本地用ip连接mysql响应快, 今天碰到一个连接mysql的怪问题,我在php 环境本地用ip连接mysql响应快,用localhost连接慢,后来百度 ...

  5. ThinkPad如何修改fn键默认操作

    ThinkPad如何修改fn键默认操作 ThinkPad笔记本如何修改fn键默认操作 Fn键F1-F12

  6. alibaba dexposed初步解析

    alibaba新出了一个非侵入的aop库,感觉不错.那么楼主这次就来学习一下这个库的详细应用,原理以及能够达到的效果. 这里先给出相应的githubproject传送门:https://github. ...

  7. 机器学习数学基础- gradient descent算法(上)

    为什么要了解点数学基础 学习大数据分布式计算时多少会涉及到机器学习的算法,所以理解一些机器学习基础,有助于理解大数据分布式计算系统(比如spark)的设计.机器学习中一个常见的就是gradient d ...

  8. php.ini中的pcre

    当页面代码没有任何问题,也没有报错.但是页面的循环很多,请求导致开销很大的时候,有可能要改php.ini中的pcre部分. [Pcre]pcre.recursion_limit=-1pcre.back ...

  9. windows环境安装tensorflow

    本机环境win7,想要安装tensorflow,试了好久,才装好.官网才是王道. 注意:目前tensorflow 在 windows 环境中仅支持 python 3.5. *64,.所以python版 ...

  10. servlet响应解析

    response对象可以设置一些响应信息 1)设置状态码 response.setStatus(int) 2)设置响应头信息.定时刷新或者间隔 n 秒后跳转 response.setHeader(&q ...