web开发下的各种下载方法
利用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);
IO的WriteFile实现下载

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分块下载

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

//流方式下载

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

最后一种支持断点下载

/// <summary>
/// 支持断点续传的下载大文件的函数
/// </summary>
/// <param name="downloadFilePath">被下载文件的路径,相对和绝对都可以</param>
/// <param name="showFileName">显示给用户的文件名,可以为空</param>
/// <param name="fileType">要下载的文件类型,如果不确定,请留空</param>
/// <remarks>用于大文件的下载</remarks>
public static void BigFileDownload(string downloadFilePath, string showFileName, string fileType)
{
downloadFilePath = downloadFilePath.Trim();
if(string.Empty == downloadFilePath)
ShowErrorMessage("没有指定要下载的文件路径!"); if(downloadFilePath.IndexOf(":") < )
downloadFilePath = System.Web.HttpContext.Current.Server.MapPath(downloadFilePath); showFileName = showFileName.Trim();
if(string.Empty == showFileName)
showFileName = Path.GetFileName(downloadFilePath); showFileName = System.Web.HttpUtility.UrlEncode(System.Web.HttpContext.Current.Request.ContentEncoding.GetBytes(showFileName)); fileType = fileType.Trim();
if(string.Empty == fileType)
fileType = "application/octet-stream"; System.Web.HttpContext.Current.Response.Clear();
Stream objStream = null;
byte[] btBuffer = new byte[]; // 1024*8 = 8Kb
int intLength = ;
long lngData2Read = ;
try
{
using(objStream = new FileStream(downloadFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
lngData2Read = objStream.Length; 输出HTTP头信息 while(lngData2Read > )
{
if(HttpContext.Current.Response.IsClientConnected)
{
intLength = objStream.Read(btBuffer, , );
HttpContext.Current.Response.OutputStream.Write(btBuffer, , intLength);
HttpContext.Current.Response.Flush();
lngData2Read -= intLength;
btBuffer = new byte[];
}
else
{
lngData2Read = -;
}
}
}
}
catch(Exception ex)
{
ShowErrorMessage(ex.Message);
}
// 结束文件下载,此句不可缺少,否则会把本页的HTML代码一起下载文件里
HttpContext.Current.Response.End();
} /// <summary>
/// 输出错误信息给用户,采用客户端弹出框的方式
/// </summary>
/// <param name="errorMessage">错误信息的内容</param>
internal static void ShowErrorMessage(string errorMessage)
{
errorMessage = errorMessage.Replace("'", "\"");
System.Web.HttpContext.Current.Response.Write("<script language='javascript'>");
System.Web.HttpContext.Current.Response.Write("alert('");
System.Web.HttpContext.Current.Response.Write(errorMessage);
System.Web.HttpContext.Current.Response.Write("');</script>");
System.Web.HttpContext.Current.Response.End();
}

web开发下的各种下载方法的更多相关文章
- 提高 Web开发性能的 10 个方法
随着网络的高速发展,网络性能的持续提高成为能否在芸芸App中脱颖而出的关键.高度联结的世界意味着用户对网络体验提出了更严苛的要求.假如你的网站不能做到快速响应,又或你的App存在延迟,用户很快就会移情 ...
- web开发中会话跟踪的方法
1. 什么是会话 会话是指一个终端用户(服务器)与交互系统(客户端)进行通讯的过程. 2. 什么是会话跟踪 对同一个用户对服务器的连续的请求和接受响应的监视.(将用户与同一用户发出的不同请求之间关联, ...
- web开发中会话跟踪的方法有哪些
会话跟踪就是浏览器和服务器通信 1.cookie 2.session 3.隐藏input 4.url重写 5.ip地址
- Linux下搭建SVN服务器及自动更新项目文件到web目录(www)的方法
首先搭建SVN服务器 1,安装SVN服务端 直接用apt-get或yum安装subversion即可(当然也可以自己去官方下载安装) sudo apt-get install subversion ...
- 【特别推荐】Web 开发人员必备的经典 HTML5 教程
对于我来说,Web 前端开发是最酷的职业之一,因为你可以用新的技术发挥,创造出一些惊人的东西.唯一的问题是,你需要跟上这个领域的发展脚步,因此,你必须不断的学习,不断的前进.本文将分享能够帮助您快速掌 ...
- web开发中目录路径问题的解决
web开发当中,目录路径的书写是再常用不过了,一般情况下不会出什么问题,但是有些时候出现了问题却一直感到奇怪,所以这里记录一下,彻底解决web开发中路径的问题,开发分为前端和服务端,那么就从这两个方面 ...
- 简明易懂,将细节隐藏,面向新手树立web开发概念——学完Java基础语法,超快速上手springboot+mybatiJavaWeb开发
简明易懂,将细节隐藏,面向新手树立web开发概念 --学完Java基础语法,超快速上手JavaWeb开发 Web本质(先忽视各种协议) Web应用可以理解为浏览器和服务器之间的交互. 我们可以看一个简 ...
- windows下python web开发环境的搭建
windows下python web开发环境: python2.7,django1.5.1,eclipse4.3.2,pydev3.4.1 一. python环境安装 https://www.pyth ...
- 《Node.js入门》CentOS 6.5下Node.js Web开发环境搭建笔记
近期想尝试一下英特尔的基于WebRTC协同通信开发套件,所以须要在本地搭建Node.js Web的开发測试环境. 这里讲的是CentOS 下的搭建方法.使用Windows的小伙伴请參考: <No ...
随机推荐
- Cocos手游录制插件:cocos-plugin
Cocos手游录制插件:cocos-plugin Testinlab2014-10-29 13:42:27153 次阅读 Cocos手游录制插件,用于添加Testin手游自动化测试支持,支持cocos ...
- ubuntu 安装ssh-server时出现错误:openssh-server: Depends: openssh-client (= 1:5.3p1-3ubuntu3) but 1:5.3p1-3ubuntu4 is to be installed
错误如下: tiger@ubuntu:~/Desktop/work$ sudo apt-get install openssh-server [sudo] password for tiger: Re ...
- mysql中间件研究(Atlas,cobar,TDDL)
mysql-proxy是官方提供的mysql中间件产品可以实现负载平衡,读写分离,failover等,但其不支持大数据量的分库分表且性能较差.下面介绍几款能代替其的mysql开源中间件产品,Atlas ...
- HDU 3696 Farm Game(dp+拓扑排序)
Farm Game Problem Description “Farm Game” is one of the most popular games in online community. In t ...
- Delphi Data Types
http://docwiki.embarcadero.com/RADStudio/XE6/en/Delphi_Data_Types Integer Data Types Type Descriptio ...
- Uva110 Meta-Loopless Sorts
Meta-Loopless Sorts Background Sorting holds an important place in computer science. Analyzing and i ...
- 玩转iOS开发 - 多线程开发
前言 本文主要介绍iOS多线程开发中使用的主要技术:NSOperation, GCD. NSThread, pthread. 内容依照开发中的优先推荐使用的顺序进行介绍,涉及多线程底层知识比較多的NS ...
- wp8 在OnBackKeyPress事件中调用MessageBox.Show()崩溃
今天写代码的时候遇到一个问题,在wp8中执行下面的代码后,弹出对话框后,停滞一段时间程序退出. protected override void OnBackKeyPress(CancelEventAr ...
- C++ 转型
1.const_static的使用场景:接收一个const对象,但是想改变对象内容,使用const_static去除对象的常量性,然后可以修改对象. 2.dynamic_static的使用场景:从子类 ...
- 【转】SoapUI5.0创建WebService接口模拟服务端
原文:http://blog.csdn.net/a19881029/article/details/26348627 使用SoapUI创建WebService接口模拟服务端需要接口描述文件 MathU ...