Asp.Net--下载文件
实现方式1:
protected void DownLoad_Click(object sender, EventArgs e)
{
//获取要下载的文件
string filename = Server.MapPath("~/upload/计算机科学与技术.rar");
FileInfo f = new FileInfo(filename); //设置文件头
Response.ContentType = "application/zip"; //对文件名进行编码处理,并设置保存时的默认文件名
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(f.Name, System.Text.Encoding.UTF8)); //输出文件
Response.TransmitFile(filename); }
效果:
实现方式2:
protected void DownLoad_Click(object sender, EventArgs e)
{
//获取要下载的文件
string file = Server.MapPath("~/upload/DesignPattern.rar");
FileInfo f = new FileInfo(file); //清空缓冲区内容
Response.Clear(); //设置文件头
Response.AddHeader("Content-Disposition", "attachment;filename="+HttpUtility.UrlDecode(f.Name,System.Text.Encoding.UTF8));
Response.AddHeader("Content-Length", f.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/zip";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); Response.WriteFile(f.FullName);
Response.End();
}
下载中文名文件时存在问题.
效果:![]()
实现方式3:
protected void DownLoad_Click(object sender, EventArgs e)
{
//获取要下载的文件,并将数据读入数组
string filepath = Server.MapPath("~/upload/DesignPattern.rar");
FileInfo fi = new FileInfo(filepath);
FileStream fs = new FileStream(filepath, FileMode.Open);
int filelength = (int)fs.Length;
byte[] bt = new byte[filelength];
fs.Read(bt, 0, filelength);
fs.Close(); //设置文件头及保存时的文件名
Response.ContentType = "application/zip";
Response.AddHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlDecode(fi.Name,System.Text.Encoding.UTF8));
Response.AddHeader("Content-Length", filelength.ToString()); //输出文件
Response.BinaryWrite(bt);
Response.Flush();
Response.End();
}
下载中文名文件时存在问题.
效果:![]()
实现方式4:
protected void DownLoad_Click(object sender, EventArgs e)
{
string filepath = Server.MapPath("~/upload/DesignPattern.rar");
FileStream fs = new FileStream(filepath, FileMode.Open);
int filelength = (int)fs.Length;
byte[] b = new byte[filelength];
fs.Read(b, 0, filelength);
fs.Close(); Response.ContentType = "application/zip";
Response.AddHeader("Content-Disposition", "attachment;filename=1.rar");
Response.AddHeader("Content-Length", filelength.ToString()); Response.OutputStream.Write(b, 0, filelength);
Response.OutputStream.Close();
Response.Flush();
Response.Close();
}
实现方式5:
protected void DownLoad_Click(object sender, EventArgs e)
{
//鑾峰彇瑕佷笅杞界殑鏂囦欢,骞跺皢鏁版嵁璇诲叆鏁扮粍
string filepath = Server.MapPath("~/upload/aspnetmvc-stepbystep.rar");
FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
long filelength = fs.Length;
int readsize = 102400;
byte[] b = new byte[readsize]; Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=1.rar");
Response.AddHeader("Content-Length", filelength.ToString()); while (filelength > 0 && Response.IsClientConnected)
{
int receive = fs.Read(b, 0, readsize);
Response.OutputStream.Write(b, 0, receive);
Response.Flush();
b = new byte[readsize];
filelength = filelength - receive;
}
fs.Close();
Response.OutputStream.Close();
Response.Close();
}
注意:
Response.AppendHeader("content-disposition", "attachment;filename=" + filename);//附件下载
Response.AppendHeader("content-disposition", "online;filename=" + filename);//在线打开
Asp.Net--下载文件的更多相关文章
- Asp.Net 下载文件的几种方式
asp.net下载文件几种方式 protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新的方法 ...
- asp.net下载文件几种方式
测试时我以字符流的形式下载文件,可行,前几个仅作参考 protected void Button1_Click(object sender, EventArgs e) { /* 微软为Respo ...
- Asp.net下载文件
网站上的文件是临时文件, 浏览器下载完成, 网站需要将其删除. 下面的写法, 文件读写后没关闭, 经常删除失败. /// <summary> /// 下载服务器文件,参数一物理文件路径(含 ...
- asp.net 下载文件(图片、word、excel等)
string filePath = Server.MapPath("~/excel.xlsx"); if (File.Exists(filePath)) { FileStream ...
- ASP.NET 下载文件方式
protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新的方法TransmitFile来解决使 ...
- asp.net 下载文件几种方式
protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新的方法TransmitFile来解决使 ...
- ASP.NET 下载文件并继续执行JS解决方法
需求说明:当用户点击按钮时使当前按钮为不可用,并打开新页面,关闭新页面时,按钮变为可用.并且如果不关闭新页面,当前按钮过10秒钟自动变为可用. 包含3个页面: 一.按钮页 前台代码:当刷新后采用js进 ...
- asp.net下载文件方法
/// <summary> /// 下载 /// </summary> /// <param name="url"></param> ...
- asp.net下载文件的几种方法
最近做东西遇到了下载相关的问题.在这里总结一下自己处理的方法. 1.以字节流的形式向页面输出数据以下载Excel为例子. string path=Server.MapPath("文件路径&q ...
- 解决用ASP.NET下载文件时,文件名为乱码的问题
关键就一句: string strTemp = System.Web.HttpUtility.UrlEncode(strName, System.Text.Enc ...
随机推荐
- 自己寫的 Loading JS插件
本文為原創文章,轉載請注明出處,謝謝./** * @author samkin.yang * @version 1.0 */var $_yxj = new SamkinLoading(); (func ...
- c++ bitset类的使用和简介
http://blog.163.com/lixiangqiu_9202/blog/static/53575037201251121331412/
- ELK( ElasticSearch+ Logstash+ Kibana)分布式日志系统部署文档
开始在公司实施的小应用,慢慢完善之~~~~~~~~文档制作 了好作运维同事之间的前期普及.. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 软件下载地址: https://www.e ...
- 人工神经网络简介和单层网络实现AND运算--AForge.NET框架的使用(五)
原文:人工神经网络简介和单层网络实现AND运算--AForge.NET框架的使用(五) 前面4篇文章说的是模糊系统,它不同于传统的值逻辑,理论基础是模糊数学,所以有些朋友看着有点迷糊,如果有兴趣建议参 ...
- ETL工具框架开源软件
http://www.oschina.net/project/tag/453/etl 开源ETL工具 Kettle Talend KETL CloverETL Apatar Scriptella ET ...
- Cookie和Session(转)
会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...
- HDOJ 1197 Specialized Four-Digit Numbers
Problem Description Find and list all four-digit numbers in decimal notation that have the property ...
- POJ_3009——冰球,IDS迭代加深搜索
Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But th ...
- cocos2d-x 几何绘制: DrawingPrimitives 和 CCDrawNode
在看书的时候只提到了DrawingPrimitives,然后我去搜索这个类,结果没搜到.心想难道是类名改了,那我搜方法名吧,搜了下DrawLine,果然被我搜到了.结果发现原来这些各方法都是全局函数, ...
- Spark standalone安装(最小化集群部署)
Spark standalone安装-最小化集群部署(Spark官方建议使用Standalone模式) 集群规划: 主机 IP ...