ASP.NET C# 文件下载
1.文件下载到客户端
//WriteFile实现下载
protected void Download_Click(object sender, EventArgs e)
{
string fileName = "20151223Test.doc";//客户端保存的文件名
//string filePath = Server.MapPath("DownLoad/aaa.txt");//路径
string filePath = Server.MapPath(@"files\test.doc");//路径
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.ContentEncoding = System.Text.Encoding.UTF8;
Response.WriteFile(fileInfo.FullName);
Response.Flush();
Response.End();
}
//TransmitFile实现下载
protected void DownLoadTF_Click(object sender, EventArgs e)
{
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename=z.zip");
string filename = Server.MapPath(@"files\DownloadsText.zip");
Response.TransmitFile(filename);
//Response.TransmitFile 需要 :Microsoft .NET Framework 1.1 Service Pack 1 支持!!
}
//流方式下载
protected void DownLoadFL_Click(object sender, EventArgs e)
{
string fileName = "20151223aaa.doc";//客户端保存的文件名
//string filePath = Server.MapPath("DownLoad/aaa.txt");//路径
string filePath = Server.MapPath(@"files\租户装修手册-印象城.doc");//路径
//以字符流的形式下载文件
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
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();
}
}
//流方式下载 2
protected void DownLoadFL2_Click(object sender, EventArgs e)
{
string fileName = "20151223aaa.doc";//客户端保存的文件名
//string filePath = Server.MapPath("DownLoad/aaa.txt");//路径
string filePath = Server.MapPath(@"files\租户装修手册-印象城.doc");//路径
//以字符流的形式下载文件
using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
byte[] bytes = new byte[(int)fs.Length];
using (BinaryWriter bw = new BinaryWriter(fs))
{
bw.Write(bytes);
bw.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();
}
}
}
ASP.NET C# 文件下载的更多相关文章
- 【转载】ASP.NET实现文件下载的功能
文件下载是很多网站中含有的常用功能,在ASP.NET中可以使用FileStream类.HttpRequest对象.HttpResponse对象相互结合,实现输出硬盘文件的功能.该方法支持大文件.续传. ...
- ASP.net MVC 文件下载的几种方法
ASP.net MVC 文件下载的几种方法(欢迎讨论) 在ASP.net MVC 中有几种下载文件的方法前提:要下载的文件必须是在服务器目录中的,至于不在web项目server目录中的文件下载我不 ...
- ASP.net MVC 文件下载的几种方法(欢迎讨论)
在ASP.net MVC 中有几种下载文件的方法 前提:要下载的文件必须是在服务器目录中的,至于不在web项目server目录中的文件下载我不知道,但是还挺想了解的. 第一种:最简单的超链接方法,&l ...
- ASP.NET 大文件下载的实现思路及代码
文件下载是一个网站最基本的功能,ASP.NET网站的文件下载功能实现也很简单,但是如果遇到大文件的下载而不做特殊处理的话,那将会出现不可预料的后果.本文就基于ASP.NET提供大文件下载的实现思路及代 ...
- (摘录)ASP.NET提供文件下载函数(支持大文件、续传、速度限制、资源占用小)
// 输出硬盘文件,提供下载 // 输入参数 _Request: Page.Request对象, _Response: Page.Response对象, _fileName: 下载文件名, _full ...
- 【基础】ASP.net MVC 文件下载的几种方法(欢迎讨论)
在ASP.net MVC 中有几种下载文件的方法 前提:要下载的文件必须是在服务器目录中的,至于不在web项目server目录中的文件下载我不知道,但是还挺想了解的. 第一种:最简单的超链接方法,&l ...
- ASP.NET实现文件下载
转:http://blog.csdn.net/codeshark/article/details/2473664 方式一:TransmitFile实现下载.将指定的文件直接写入 HTTP 响应输出流, ...
- asp.net mvc文件下载
一.zip打包下载 1.依赖引用:ICSharpCode.SharpZipLib 2.设定网站有单独文件服务器,网站目录下有虚拟路径FileFolder,通过虚拟路径将文件映射到文件服务器. 设定根据 ...
- asp.net mvc5 文件下载上传
下载:是通过点击a标签直接下载的方式,没有其他任何要求,在服务器上存在实体文件,不需要请求后台控制层 前段js: <a id="NF-DownLoad" authorize= ...
随机推荐
- SSI框架中配置log4j
事实上主要是log4j配置,跟SSI关系不大. web.xml中加入 <context-param> <param-name>log4jConfigLocation</p ...
- [MEAN+ Webstrom] First API -- 2.Debug Node.js RESTful application
Using WebStrom can easily debug the Node applcation. For example, we have an Node+Express applicatio ...
- [Express] Level 2: Middleware -- 1
Mounting Middleware Given an application instance is set to the app variable, which of the following ...
- oc-17-description
Book.h #import <Foundation/Foundation.h> @interface Book : NSObject { NSString *_bookName; // ...
- THD 变量存入threads中
http://blog.csdn.net/gapaul/article/details/12047497 http://ourmysql.com/archives/930
- linux下网络编程常见问题
网络程序异常退出无core文件产生 这种情况发生在一边连接端已经关闭,但是另外一边还在对连接句柄做send操作,这样做send操作的进程会收到SIGPIPE信号,默认行为是直接退出且不会产生core. ...
- 如何恢复 Linux 上删除的文件,第 1 部分
来源:http://www.ibm.com/developerworks/cn/linux/l-cn-filesrc/ 原理及普通文件的恢复 要想恢复误删除的文件,必须清楚数据在磁盘上究竟是如何存储的 ...
- IFormatProvider,ICustomFormatter,IFormattable总结
IFormatProvider中 public object GetFormat(Type formatType); 该方法主要用于获取一个 ICustomFormatter接口的实例 ICustom ...
- Spring SqlRowSet example--转载
原文地址:http://www.roseindia.net/tutorial/spring/spring3/jdbc/sqlrowset.html The 'SqlRowSet' is used to ...
- c#获得目标服务器中所有数据库名、表名、列名的实现代码
/// <summary> /// 获得目标服务器所有数据库名 /// </summary> /// <param name="serverName" ...