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 C#实现下载文件的六种方法实例的更多相关文章

  1. Java读取Properties文件的六种方法

    使用J2SE API读取Properties文件的六种方法 1.使用java.util.Properties类的load()方法示例: InputStream in = lnew BufferedIn ...

  2. javaSE读取Properties文件的六种方法

    使用JavaSEAPI读取Properties文件的六种方法 1.使用java.util.Properties类的load()方法 示例:InputStreamin=lnewBufferedInput ...

  3. Microsoft Edge浏览器下载文件乱码修复方法(二)

    之前有写过"Microsoft Edge浏览器下载文件乱码修复方法",发现很多情况下下载文件乱码问题还是存在,这里对之前内容做简单补充,希望可以帮到大家. 方法二: 默认如果提示下 ...

  4. java加载properties文件的六种方法总结

    java加载properties文件的六种方法总结 java加载properties文件的六中基本方式实现 java加载properties文件的方式主要分为两大类:一种是通过import java. ...

  5. asp.net core配置下载文件

    asp.net core的wwwroot文件夹下默认时保存静态文件的地方,外面可以直接访问,但是如果是一些无法识别的后缀文件,如(.apk),会报错404 如果想要实现下载这些文件,在配置静态文件中间 ...

  6. jsp下载文件的实现方法及注意事项 (转)

    jsp中实现文件下载,最简单的方式是在网页上做超级链接,如:<a href="music/abc.mp3">点击下载</a>. 但是,这样服务器上的目录资源 ...

  7. 从tomcat下载文件的配置方法(很全呢)

    前几天我做的项目有个下载文件的东西让我苦恼了一下,上传的文件没有放到OSS服务器,而是直接放到tomcat内, 我就想做一个a标签直接下载的得了,结果点开一直都说没有该文件,我查了很多资料找到了如何配 ...

  8. (转nginx不浏览直接下载文件的解决方法

    原文:https://www.zhan200.com/xwt/39.html 如果nginx配置不对,就会造成部分文件,在浏览器中不是直接预览,而是进行了下载.修改的方法是修改配置文件.具体解决方法如 ...

  9. ASP.NET MVC Ajax下载文件(使用NPOI向现有的excel模板文件里面添加数据)

    View Html.DevExpress().Button(DevExpressButtonHelper.AddButton(ViewBag.Form, "Export", &qu ...

随机推荐

  1. coffeescript 1.6.3使用帮助

    CoffeeScript is a little language that compiles into JavaScript. Underneath that awkward Java-esque ...

  2. CSS3 animation动画

    CSS3 animation动画 1.@keyframes 定义关键帧动画2.animation-name 动画名称3.animation-duration 动画时间4.animation-timin ...

  3. Java solr 分词

    代码如下: import java.io.IOException; import java.util.*; import org.apache.solr.client.solrj.SolrClient ...

  4. 基于Spark机器学习和实时流计算的智能推荐系统

    概要: 随着电子商务的高速发展和普及应用,个性化推荐的推荐系统已成为一个重要研究领域. 个性化推荐算法是推荐系统中最核心的技术,在很大程度上决定了电子商务推荐系统性能的优劣,决定着是否能够推荐用户真正 ...

  5. Solr删除数据

    步骤: 1.在Solr客户端左下方 Core Selector 中点选想要删除数据的索引库 2.点选Documents 3.右侧Document Type中点选XML 4.Document(s)中输入 ...

  6. Activemq消息确认机制 --转载

      转自:http://blog.csdn.net/czp11210/article/details/47022639 ActiveMQ消息传送机制以及ACK机制详解 AcitveMQ是作为一种消息存 ...

  7. python基础语法(四)

    --------------------------------------------接 Python 基础语法(三)---------------------------------------- ...

  8. lodash kebabCase

    _.kebabCase([string='']) 转换字符串为 kebab case. _.kebabCase('Foo Bar'); // => 'foo-bar' _.kebabCase(' ...

  9. 经常使用传感器协议3:CJ/T-188 冷热量表协议解析2

        本文详细阐述JY公司冷热量表(记热量)传输协议.并以此说明CJ/T-188协议在厂家详细应用时,并不一致. 本文及兴许文章将对这些不同点予以总结(文中所述协议与日志"CJ/T-188 ...

  10. 用C#实现XML和实体类之间序列化和反序列化相互转换

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...