Asp.Net 之 下载文件的常用方式
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 之 下载文件的常用方式的更多相关文章
- asp.net mvc5 下载文件方法
控制器自带的 FileContentResult 可以让我们很方便的返回文件到服务端,减少了很多步骤.用于下载文件的时候,像视频.文本.图片这种浏览器支持的文件,默认就会被浏览器打开.这时候想让它变成 ...
- 分布式进阶(十) linux命令行下载文件以及常用工具:wget、Prozilla、MyGet、Linuxdown、Curl、Axel
linux命令行下载文件以及常用工具:wget.Prozilla.MyGet.Linuxdown.Curl.Axel 本文介绍常用的几种命令行式的下载工具:wget.Prozilla.MyGet.Li ...
- asp.net下载文件几种方式
测试时我以字符流的形式下载文件,可行,前几个仅作参考 protected void Button1_Click(object sender, EventArgs e) { /* 微软为Respo ...
- asp.net 浏览器下载文件的四种方式
// 方法一:TransmitFile实现下载 protected void Button1_Click(object sender, EventArgs e) { Response.ContentT ...
- asp.net 下载文件几种方式
protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新的方法TransmitFile来解决使 ...
- C# ASP 上传/下载文件
1. 上传文件前台页面 <div style="padding-left:20px;"> <asp:FileUpload ID="FileUpload ...
- .net 下载文件几种方式
方式一:TransmitFile实现下载.将指定的文件直接写入 HTTP 响应输出流,而不在内存中缓冲该文件. protected void Button1_Click(object sender, ...
- java 读取文件的常用方式
1.读取: public class ReadFromFile { /** * 以字节为单位读取文件,常用于读二进制文件,如图片.声音.影像等文件. */ public static void rea ...
- Asp.net mvc 下载文件
前言 最近有需求需要下载文件,可能是image的图片,也可能是pdf报告,也可能是微软的word或者excel文件. 这里就整理了asp.net mvc 和asp.net webapi 下载的方法 A ...
随机推荐
- 使用struts的同步令牌避免form的重复提交
struts1避免重复提交 一.使用方法 1. 假如你要提交的页面为toSubmit.jsp: 2. 在打开toSubmit.jsp的Action1中加入:saveToken(request),例 ...
- the type initializer for '' threw an exception
the type initializer for '' threw an exception 问题:程序启动时初始化主窗口类时,弹出该错误.调查:查看类的构造函数是否会有异常抛出.解决:去掉类的构造函 ...
- MFC无边框窗体不响应任务栏点击问题
为了提升用户体验,需要隐藏主窗体的边框,使用图片绘制新的标题栏.标题栏绘制之后,发现用户点击任务栏上应用程序的图标,应用程序不会随着点击交替隐藏显示. 分析结果是问题出现窗体风格设置上. 最初为了省事 ...
- POJ 1151 Atlantis 线段树+离散化+扫描线
这次是求矩形面积并 /* Problem: 1151 User: 96655 Memory: 716K Time: 0MS Language: G++ Result: Accepted */ #inc ...
- Mac 中查看端口占用进程并杀死
sudo lsof -i :9000 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME java 61342 a 313u IPv6 0x11111 ...
- Ubuntu firefox falsh
Ubuntu下为Firefox安装Adobe Flash Player 使用环境: OS:Ubuntu 12.04 LTS Browser: Firefox 12.0 Ad ...
- win7 开wifi热点
开启windows 7的隐藏功能:虚拟WiFi和SoftAP(即虚拟无线AP),就可以让电脑变成无线路由器,实现共享上网,节省网费和路由器购买费. 1.启用并设定虚拟WiFi网卡: 运行命令:nets ...
- HDU-4618 Palindrome Sub-Array 暴力枚举
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4618 直接暴力枚举中心点,在中间如果求不出最大值直接跳过优化下... //STATUS:C++_AC_ ...
- Redis的探究
之前大四时候实习的公司有使用过Redis,不过那时所有配置均由主管完成了,而我也只是处于能使用的阶段. 时隔1年多,近期回想起这货,在研究中,想把它整合进现在公司的网站系统中,做做数据快照(已完成)和 ...
- ubuntu 运行android sdk 下的工具adb报bash: ./adb: No such file or directory
运行adb出现这种错误: bash: ./adb: No such file or directory 但adb确实存在. 可能1:你用的是64位的Linux,没装32位运行时库,安装 $ sud ...