.net中下载文件的方法
一、//TransmitFile实现下载
protected void Button1_Click(object sender, EventArgs e)
{
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();
}
转载于:https://www.cnblogs.com/Mr-Joe/archive/2011/11/22/2258394.html
.net中下载文件的方法相关推荐
- .net中下载文件的方法(转)
.net中下载文件的方法 一.//TransmitFile实现下载 protected void Button1_Click(object sender, EventArgs e) ...
- 【vue】vue中下载文件的方法
文章目录 1. 下载后端返回文件 1.1 后端为post请求返回二进制流文件 URL.createObjectURL FileReader 1.2 后端直接返回get请求文件 2. 下载本地文件 1. ...
- 两种 js下载文件的方法(转)
2019独角兽企业重金招聘Python工程师标准>>> 两种 js下载文件的方法(转) functionDownURL(strRemoteURL, strLocalURL){try{ ...
- linux中下载文件的命令
2019独角兽企业重金招聘Python工程师标准>>> 1 wget wget是linux最常用的下载命令, 一般的使用方法是: wget + 空格 + 要下载文件的url路径 例如 ...
- 爬虫(21)crawlspider讲解古诗文案例补充+小程序社区案例+汽车之家案例+scrapy内置的下载文件的方法
文章目录 第十九章 crawlspider讲解 1. 古诗文案例crawlspider 1.1 需求 1.2 处理 1.3 解析 2. 小程序社区案例 2.1 创建项目 2.2 项目配置 2.3 解析 ...
- vue中下载文件导出保存到本地
vue中下载文件导出保存到本地 先分析如何下载:先有一个链接地址,然后使用 location.href或window.open()下载到本地 看看返回数据 res.config.url 中是下载链接地 ...
- python多线程下载大文件_Python threading多线程断点下载文件的方法
这是玩蛇网一篇关于Python多线程下载文件方法的代码实例.文中应用到的python模块和方法有httplib.Python urllib2.Python threading多线程模块.python ...
- Kaggle从google drive下载文件的方法
文章目录 1. 从google drive获取文件ID: 1.1 在google drive中右键点击文件,选择`获取链接` 1.2 在弹窗中复制链接 1.3 修改权限 2. 在kaggle note ...
- java如何从https下载链接中下载文件
java如何从https下载链接中下载文件 文章目录 java如何从https下载链接中下载文件 从https 下载文件会存在什么用的问题? 导入安全证书到jdk 下载文件 URL 获取inputSt ...
最新文章
- 车险赔付率分析报告_车险改革究竟是涨价还是降价了?9月19号后买会便宜吗?...
- linux系统中扩展一个逻辑卷,Linux 创建及扩展逻辑卷
- python真的可以减少工作强度_用Python写几行代码,一分钟搞定一天工作量,同事直呼:好家伙!...
- 打jar包和执行jar包
- iOS开发中runtime介绍
- 深入理解Solaris内核中互斥锁(mutex)与条件变量(condvar)之协同工作原理
- org.apache.hadoop.hbase.PleaseHoldException: Master is initializing
- 我Web前端开发的开端
- hyper-v虚拟机驱动_如何在Hyper-V虚拟机中访问本地和USB硬盘驱动器
- Notepad++无法修改中文解决办法
- 思维导图使用技巧:手把手教你怎么画思维导图
- 北京理工大学软件工程复试之路
- php怎么使用sendcloud,请教大牛们 PHPHub 使用 sendcloud 发送邮件需要怎么配置?
- 抓包神器:Charles
- 客户管理系统CRM推荐
- 如何复制那些无法复制的网站
- Tablestore结合Spark的流批一体SQL实战
- UCOS学习(一)——前后台系统、RTOS系统
- ckeditor+vue 傻瓜式操作教程
- 数学实验课MATLAB实验报告二(题目+代码)
热门文章
- python开发网站的优势_Python开发 的优势在哪里
- element ui 批量删除之后动态更新列表_气象编程 | Python高效批量绘图方法
- 基类与派生类之间的转换关系
- 笔记-项目合同管理-索赔的处理
- Geoserver中TileLayers中切割离线瓦片预览时地图模糊不清
- 若依微服务版后台服务通过jar包部署到Windows服务器
- C#中实现文件重命名的方式
- SSM中使用Druid连接池
- [论文翻译]Pedestrian Alignment Network for Large-scale Person Re-Identification
- 计算机网络总结:第五章 链路层