C#从服务器下载文件的四种方式
//方法一:TransmitFile实现下载
string fileName = "ss.docx"; //客户端预设的文件名,导出时可修改
string filePath = Server.MapPath("~/AssessReport/评估报表.docx"); //目标文件路径
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.TransmitFile(filePath);
注释:微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题
//方法二:WriteFile实现下载
string fileName = "ss.docx"; //客户端保存的文件名
string filePath = Server.MapPath("~/AssessReport/评估报表.docx"); //目标文件路径
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 = "ss.docx"; //客户端保存的文件名
string filePath = Server.MapPath("~/AssessReport/评估报表.docx");
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=" + 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();
}
//方法四:流方式下载
string fileName = "aaa.docx"; //客户端保存的文件名
string filePath = Server.MapPath("~/AssessReport/评估报表.docx");
//以字符流的形式下载文件
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();
C#从服务器下载文件的四种方式的更多相关文章
- asp.net 浏览器下载文件的四种方式
// 方法一:TransmitFile实现下载 protected void Button1_Click(object sender, EventArgs e) { Response.ContentT ...
- C++服务器下载文件的两种方式
#include <afxinet.h>#include "wininet.h" #pragma comment( lib, "wininet.lib&quo ...
- linux服务器之间传输文件的四种方式
linux文件传输在内网渗透中至关重要,所以我在此总结一下几种Linux服务器之间传输文件的四种方式 1. scp [优点]简单方便,安全可靠:支持限速参数[缺点]不支持排除目录[用法]scp就是se ...
- java读取XML文件的四种方式
java读取XML文件的四种方式 Xml代码 <?xml version="1.0" encoding="GB2312"?> <RESULT& ...
- 从后端接口下载文件的2种方式:get方式、post方式
从后端接口下载文件的2种方式 一.get方式 直接使用: location.href='http://www.xxx.com/getFile?params1=xxx¶ms2=xxxx' ...
- 【文件下载】Java下载文件的几种方式
[文件下载]Java下载文件的几种方式 摘自:https://www.cnblogs.com/sunny3096/p/8204291.html 1.以流的方式下载. public HttpServl ...
- linux创建文件的四种方式(其实是两种,强行4种)
linux创建文件的四种方式: 1.vi newfilename->i->编辑文件->ESC->:wq! 2.touch newfilename 3.cp sourcePath ...
- 解析xml文件的四种方式
什么是 XML? XML 指可扩展标记语言(EXtensible Markup Language) XML 是一种标记语言,很类似 HTML XML 的设计宗旨是传输数据,而非显示数据 XML 标签没 ...
- spring引用hibernate映射文件的四种方式
1.mappingResources 2.mappingLocations 3.mappingDirectoryLocations 4.mappingJarLocations 四种方式:https:/ ...
随机推荐
- JS函数传递字符串参数(符号转义)
原文链接:https://blog.csdn.net/Myname_China/article/details/82907965 JS函数传递字符串参数,如果没有转义处理,在接收的时候无法正确的接收字 ...
- OpenResty之指令与常用API
1. 指令 通过 Lua 编写 Nginx 脚本的基本构建块是指令.指令常用于指定 Lua 代码是几时执行的以及如何使用运行的结果.下图展示了指令执行的顺序. lua_capture_error_lo ...
- 第11组 Beta冲刺(4/5)
第11组 Beta冲刺(4/5) 队名 不知道叫什么团队 组长博客 https://www.cnblogs.com/xxylac/p/12018586.html 作业博客 https://edu. ...
- vue cli 安装element-ui
1.安装elment-ui --save 参数:上线打包 MacBookPro:vue_test zhangxm$ npm install element-ui axios --save npm WA ...
- Installing the Solidity Compiler¶
Versioning¶ Solidity versions follow semantic versioning and in addition to releases, nightly develo ...
- php判断为空就插入,判断不为空就更新
if ($_GET['tplname']!==null) { if ($userinfo[0] == ''){$exec="INSERT INTO cblej_company_pc_temp ...
- git clone https://chromium.googlesource.com/失败
一.现象 连接着vpn,网页上可以直接打开网站,但是使用terminal 执行git clone https://chromium.googlesource.com/xxxx时, 报错 ...
- 实现两个DataTable的联合查询
如方法一描述:将子表的数组追加到主表数组的下面.从而实现类似于视图(单表)的效果. 那么Left Join(Inner Join)和Right Join(Outer Join) 将如何实现呢? 明天仔 ...
- 福昕PDF电子文档处理套装软件中文企业版9.01
下载地址:http://zbh.ustc.edu.cn/msiso/FoxitPDFEditor901_ZH_Setup.msi 激活码:A7000-010S0-RC900-XVF4R-9J5OM-W ...
- 【ARM-Linux开发】U-Boot启动过程--详细版的完全分析
---------------------------------------------------------------------------------------------------- ...