.net中下载文件的方法(转)
.net中下载文件的方法
一、//TransmitFile实现下载 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/z.zip"); Response.TransmitFile(filename); }
二、//WriteFile实现下载 protected void Button2_Click(object sender, EventArgs e) { /* using System.IO;
*/ string fileName = "asd.txt";//客户端保存的文件名 string filePath = Server.MapPath("DownLoad/aaa.txt");//路径
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.txt";//客户端保存的文件名 string filePath = Server.MapPath("DownLoad/aaa.txt");//路径
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.txt";//客户端保存的文件名 string filePath = Server.MapPath("DownLoad/aaa.txt");//路径
//以字符流的形式下载文件 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(); }
//----------------------------------------------------------
public void DownloadFile( System.Web.UI.Page WebForm,String FileNameWhenUserDownload ,String FileBody ) {
WebForm.Response.ClearHeaders(); WebForm.Response.Clear(); WebForm.Response.Expires = 0; WebForm.Response.Buffer = true; WebForm.Response.AddHeader("Accept-Language", "zh-tw"); //'文件名称 WebForm.Response.AddHeader("content-disposition", "attachment; filename='"+System.Web.HttpUtility.UrlEncode(FileNameWhenUserDownload, System.Text.Encoding.UTF8)+"'"); WebForm.Response.ContentType = "Application/octet-stream"; //'文件内容 WebForm.Response.Write(FileBody);//----------- WebForm.Response.End(); }
//上面这段代码是下载一个动态产生的文本文件,若这个文件已经存在于服务器端的实体路径,则可以通过下面的函数:
public void DownloadFileByFilePath( System.Web.UI.Page WebForm,String FileNameWhenUserDownload ,String FilePath ) { WebForm.Response.ClearHeaders(); WebForm.Response.Clear(); WebForm.Response.Expires = 0; WebForm.Response.Buffer = true; WebForm.Response.AddHeader("Accept-Language", "zh-tw"); //文件名称 WebForm.Response.AddHeader("content-disposition", "attachment; filename='" + System.Web.HttpUtility.UrlEncode(FileNameWhenUserDownload, System.Text.Encoding.UTF8) +"'" ); WebForm.Response.ContentType = "Application/octet-stream"; //文件内容 WebForm.Response.Write(System.IO.File.Rea}dAllBytes(FilePath));//--------- WebForm.Response.End();
}
.net中下载文件的方法(转)的更多相关文章
- Javaweb向服务器上传文件以及从服务器下载文件的方法
先导入jar包 点击下载 commons-fileupload是Apache开发的一款专门用来处理上传的工具,它的作用就是可以从request对象中解析出,用户发送的请求参数和上传文件的流. comm ...
- linux 从百度网盘下载文件的方法
linux 从百度网盘下载文件的方法 发表于2015 年 月 日由shenwang 方法1.wget wget是在Linux下开发的开放源代码的软件,作者是Hrvoje Niksic,后来被移植到包括 ...
- VSTO学习笔记(四)从SharePoint 2010中下载文件
原文:VSTO学习笔记(四)从SharePoint 2010中下载文件 上一次我们开发了一个简单的64位COM加载项,虽然功能很简单,但是包括了开发一个64位COM加载项的大部分过程.本次我们来给CO ...
- js中使用showModelDialog中下载文件的时候,闪一下后无法下载
在js中使用showModelDialog中下载文件的时候,会因为showModelDialog自动设置target为_self导致下载文件“只会闪一下”就消失掉 在吧target设置为_blank后 ...
- django 中下载文件与下载保存为excel
一.django 中下载文件 在实际的项目中很多时候需要用到下载功能,如导excel.pdf或者文件下载,当然你可以使用web服务自己搭建可以用于下载的资源服务器,如nginx,这里我们主要介绍dja ...
- SpringMVC实现从磁盘中下载文件
除了文件的上传我们还需要从磁盘下载 实现文件的下载只要编写一个控制器,完成读写操作和响应头和数据类型的设置就可以了 下面演示的是从G盘imgs文件夹中下载文件 具体代码如下 package com.c ...
- java 从网络Url中下载文件 (转)
http://blog.csdn.net/xb12369/article/details/40543649/ /** * 从网络Url中下载文件 * @param urlStr ...
- 使用curl在命令行中下载文件
http://m.blog.csdn.net/blog/mayadong7349/7019208 使用curl在命令行中下载文件 linux下curl简单应用详解 http://blog.sina.c ...
- java 从网络Url中下载文件
转自:http://blog.csdn.net/xb12369/article/details/40543649 /** * 从网络Url中下载文件 * @param urlStr * @param ...
随机推荐
- 【转】OS X Mavericks: 防止 Mac 进入睡眠 -- 不错
原文网址:https://support.apple.com/kb/PH13808?locale=zh_CN&viewlocale=zh_CN 某些 Mac 电脑将在不活跃一段时间后自动进入睡 ...
- 提取WORD中的所有InlineShape图片并保存成文件
InlineShape表示的类型其实不只是图片,还包括OLE, ACTIVE X等. 下面是MSDN中的定义:Represents an object in the text layer of a d ...
- [CODEVS1037]取数游戏
N(2 <=N<=200,且为偶数)个正整数的序列放在一个游戏平台上,A.B两人轮流从序列的两端取数,取数后该数字被去掉并累加到本玩家的得分中,当数取尽时,游戏结束.以最终得分多者为胜(A ...
- 【原】模式之-适配器Adapter模式
适配器Adapter模式 适配器模式(Adapter Pattern)把一个类的接口变换成客户端所期待的的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作. 模式所涉及的角色有 ...
- cocos2d-x 从onEnter、onExit、 引用计数 谈内存泄露问题
/////////////////////////////////// //author : zhxfl //date : 2013.8.29 //email : 291221622@qq.co ...
- HW4.41
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- Kooboo中怎么写Page Plugin -摘自官方文档
Page plugin development Page plugin is an add-on to Kooboo CMS, and is responsible for making data s ...
- Dynamics CRM 2011 2013-(An error occurred while opening mailbox xxx@xx.com Microsoft.Crm.Tools.Email.Providers.)
An error occurred while opening mailbox Microsoft.Crm.Tools.Email.Providers. Whenever I check how C ...
- 利用gdb 调试android jni c动态库
http://blog.dornea.nu/2015/07/01/debugging-android-native-shared-libraries/ Since I haven't done thi ...
- 从java程序员到CTO的成长路线图
很多新人不知道从事java开发,具体的发展路径是怎么样的,甚至很多人都不能区分程序猿和攻城师的区别.包括不少小白,从事java开发都半年,甚至1年了,对职业发展还没有清晰的认证.这非常不利于自己的发展 ...