1、直接使用Response.TransmitFile(filename)方法

    protected void Button_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");
     //将指定的文件直接写入 HTTP 响应输出流,而不在内存中缓冲该文件。
Response.TransmitFile(filename);
}

2、WriteFile实现下载

    protected void Button_Click(object sender, EventArgs e)
{
/* using System.IO; */ string fileName = "aaa.zip";//服务端保存的文件名
string filePath = Server.MapPath("DownLoad/aaa.zip");//路径 System.IO.FileInfo fileInfo = new System.IO.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");
//将指定文件的内容作为文件块直接写入 HTTP 响应输出流。
Response.WriteFile(fileInfo.FullName);
Response.Flush();
Response.End();
}

3、WriteFile分块下载

    protected void Button_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 = ;//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();
}
}

4、流方式下载

    protected void Button4_Click(object sender, EventArgs e)
{
string fileName = "aaa.zip";//服务端保存的文件名
string filePath = Server.MapPath("DownLoad/aaa.zip");//路径 //以字符流的形式下载文件
System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.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));
//将一个二进制字符串写入 HTTP 输出流
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}

Asp.Net 之 下载文件的常用方式的更多相关文章

  1. asp.net mvc5 下载文件方法

    控制器自带的 FileContentResult 可以让我们很方便的返回文件到服务端,减少了很多步骤.用于下载文件的时候,像视频.文本.图片这种浏览器支持的文件,默认就会被浏览器打开.这时候想让它变成 ...

  2. 分布式进阶(十) linux命令行下载文件以及常用工具:wget、Prozilla、MyGet、Linuxdown、Curl、Axel

    linux命令行下载文件以及常用工具:wget.Prozilla.MyGet.Linuxdown.Curl.Axel 本文介绍常用的几种命令行式的下载工具:wget.Prozilla.MyGet.Li ...

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

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

  4. asp.net 浏览器下载文件的四种方式

    // 方法一:TransmitFile实现下载 protected void Button1_Click(object sender, EventArgs e) { Response.ContentT ...

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

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

  6. C# ASP 上传/下载文件

    1.  上传文件前台页面 <div style="padding-left:20px;"> <asp:FileUpload ID="FileUpload ...

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

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

  8. java 读取文件的常用方式

    1.读取: public class ReadFromFile { /** * 以字节为单位读取文件,常用于读二进制文件,如图片.声音.影像等文件. */ public static void rea ...

  9. Asp.net mvc 下载文件

    前言 最近有需求需要下载文件,可能是image的图片,也可能是pdf报告,也可能是微软的word或者excel文件. 这里就整理了asp.net mvc 和asp.net webapi 下载的方法 A ...

随机推荐

  1. (四)学习CSS之position、bottom、left、right和top属性

    参考:http://www.w3school.com.cn/cssref/pr_class_position.asp position 属性规定元素的定位类型. 这个属性定义建立元素布局所用的定位机制 ...

  2. POJ 1001 Exponentiation

    题意:求c的n次幂……要求保留所有小数…… 解法:一开始只知道有BigInteger……java大数+模拟.第一次写java大数……各种报错各种exception……ORZ 没有前导0和小数后面的补位 ...

  3. java中的快捷键

    Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加)Ctrl+Alt+↑ 复制当前行到上一行(复制增加)Alt+↓ 当 ...

  4. Java was started but returned exit code=13 C:\ProgramData\Oracle\Java\javapath\javaw.exe

    ---------------------------Eclipse---------------------------Java was started but returned exit code ...

  5. MVC框架模式技术实例(用到隐藏帧、json、仿Ajax、Dom4j、jstl、el等)

    前言: 刚刚学完了MVC,根据自己的感悟和理解写了一个小项目. 完全按照MVC模式,后面有一个MVC的理解示意图. 用MVC模式重新完成了联系人的管理系统: 用户需求: 多用户系统,提供用户注册.登录 ...

  6. IoSkipCurrentIrpStackLocation .(转)

    原文链接:http://m.blog.csdn.net/blog/ruanben/19758769# 当驱动被分层以后,他们被注册到一个chain中,IRP会在这个chain中传递,从最上面,到最下面 ...

  7. JDBCTemplate.java

    package com.pk.xjgs.util; import java.sql.Connection; import java.sql.SQLException; import java.util ...

  8. 平面上画n条直线,最多能将平面分成多少部分?

    转自:http://blog.csdn.net/cywosp/article/details/6724522 在一个平面上画1999条直线,最多能将这一平面划分成多少个部分? 没有直线时有一个空间:( ...

  9. Python PIL创建文字图片

    PIL库中包含了很多模块,恰当地利用这些模块可以做许多图像处理方面的工作. 下面是我用来生成字母或字符串测试图片而写的类及测试代码. 主要用到的模块: PIL.Image,PIL.ImageDraw, ...

  10. Magento输入正确的登陆名和密码无法进入后台

    请找到 这个文件夹: app\code\core\Mage\Core\Model\Session\Abstract 然后修改Varien.php这个文件: 找到代码(大约88行): $cookiePa ...