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 下载文件的几种方式

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

  2. MVC下载文件方式

    MVC下载文件方式 http://www.cnblogs.com/liang--liang/archive/2012/10/20/2732745.html 方式一: public FileStream ...

  3. 向linux服务器上传下载文件方式收集

    向linux服务器上传下载文件方式收集 1. scp [优点]简单方便,安全可靠:支持限速参数[缺点]不支持排除目录[用法] scp就是secure copy,是用来进行远程文件拷贝的.数据传输使用 ...

  4. MVC下载文件方式 包括网络地址文件

    MVC下载文件方式 方式一: public FileStreamResult DownFile(string filePath, string fileName){      string absol ...

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

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

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

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

  7. asp.net下载文件的几种方法

    最近做东西遇到了下载相关的问题.在这里总结一下自己处理的方法. 1.以字节流的形式向页面输出数据以下载Excel为例子. string path=Server.MapPath("文件路径&q ...

  8. Asp.net下载文件

    网站上的文件是临时文件, 浏览器下载完成, 网站需要将其删除. 下面的写法, 文件读写后没关闭, 经常删除失败. /// <summary> /// 下载服务器文件,参数一物理文件路径(含 ...

  9. asp.net 下载文件(图片、word、excel等)

    string filePath = Server.MapPath("~/excel.xlsx"); if (File.Exists(filePath)) { FileStream ...

随机推荐

  1. go语言实现https的简单get和post请求

    package main import ( "crypto/tls" "fmt" "io" "io/ioutil" &q ...

  2. Java中超大文件读写

    如果文件过大不能一次加载,就可以利用缓冲区: File file = new File(filepath); BufferedInputStream fis = new BufferedInputSt ...

  3. redis与memacache的区别(转)

    redis和memecache的不同在于:1.存储方式:memecache 把数据全部存在内存之中,断电后会挂掉,数据不能超过内存大小redis有部份存在硬盘上,这样能保证数据的持久性.2.数据支持类 ...

  4. Jmeter中主要管理器功用

    不管是在使用jmeter进行性能测试还是接口自动化测试时经常用到各种管理器进行对参数的处理,但是有时候分不清几种管理器的具体用法,所以做个笔记,列一下吧(所列内容基于版本为3.2) 主要内容包含以下: ...

  5. linux的软件安装方式总结

    Linux系统中软件的“四”种安装原理详解:源码包安装.RPM二进制安装.YUM在线安装.脚本安装包   一.Linux软件包分类 1.1 源码包 优点: 开源,如果有足够的能力,可以修改源代码: 可 ...

  6. 将欢迎消息添加到PeopleSoft主页--登录事件

    以下是如何为PeopleSoft应用程序设置Signon事件消息. 导航到PeopleTools>实用程序>管理> SignOn事件消息 在此页面上,您将看到当前处于活动状态或过去处 ...

  7. PowerApp Document

    https://docs.microsoft.com/en-us/powerapps/ PowerApp Document: https://docs.microsoft.com/en-us/powe ...

  8. CSS-单位em 和 rem

    1,em单位(css3新增单位) em:就是一种长度单位,它是参照当前元素的字号,如果没有设置,就参照父容器,一直到浏览器的默认字号大小 em 是相对长度单位(参照父元素),其参照当前元素字号大小,如 ...

  9. IntelliJ IDEA配置Tomcat和Lombok

    Tomcat的安装和配置 Tomcat 是在SUN公司的JSWDK(JavaServer Web DevelopmentKit)的基础上发展而来的一个优秀的Servlet容器,其本身完全是由Java编 ...

  10. maven的安装及配置

    学习的目标 1.能够掌握Maven的安装 2.能够配置Maven仓库 3.理解Maven的依赖传递 4.能够掌握Maven工程的创建 准备工作 1.需要的资料(apache-maven-3.5.2,本 ...