.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 ...
随机推荐
- Log4net 写文件日志与数据库日志
一.数据库日志表结构 CREATE TABLE [dbo].[WebLog_Msg]( [LogID] [int] IDENTITY(1,1) NOT NULL, [Date] [datetime] ...
- [POJ2484]A Funny Game
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4533 Accepted: 2780 Description Alice ...
- 五指CMS发布,主打高性能
近日,五指CMS正式发布.给沉静已久的国内 CMS 行业引来不少的关注.五指CMS由原PHPCMS v9的负责人王参加主导开发.我们可以看到,由于移动互联网以及大数据的崛起,个人站长市场的逐渐减少,国 ...
- Linux内核系列设备模型(一) Kobject与Kset
1.Kobject Kobject是设备驱动模型的核心结构,它使所有设备在底层都有统一的接口.在内核注册的kobject对象都会对应sysfs文件系统中的一个目录(目录名称有Kobject结构中k_n ...
- 新浪微博、腾讯微博、QQ空间、人人网、豆瓣 一键分享API
新浪微博: http://service.weibo.com/share/share.php?url= count=表示是否显示当前页面被分享数量(1显示)(可选,允许为空) & url=将页 ...
- strip, 关于去除目标文件种的不必要信息
对于so动态库及可执行文件,可以直接调用不带参数的strip (-s, 即--strip-all)去除大多数不必要的信息.因为so库非常标准,所以strip之后仍然可以进行完美的动态连接:而可执行文件 ...
- hdu5593--ZYB's Tree(树形dp)
问题描述 ZYB有一颗N个节点的树,现在他希望你对于每一个点,求出离每个点距离不超过KK的点的个数. 两个点(x,y)在树上的距离定义为两个点树上最短路径经过的边数, 为了节约读入和输出的时间,我们采 ...
- dut1305 台阶
Description 如上图所示的一个台阶他的积水量是4 + 2 + 4 + 3 + 4 = 17. 给你一个长度是n的台阶.告诉你每个台阶的高度,求积水量是多少? Input 多组输入数据: 每组 ...
- opencv基础知识-videowriter
一.前言-简介 在试验中需要常常将实验结果进行保存,在opencv中提供很好用的录制视频的句柄,也可称之为类-videowriter. videowriter应用那是相当的简单,总之分为三步: //声 ...
- 使用Camstudio和KeyCastOW来录屏制作软件Demo视频
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:使用Camstudio和KeyCastOW来录屏制作软件Demo视频.