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 ...
随机推荐
- Ubuntu最小化桌面快捷键Super+D不生效解决
之前用的Debian,最近研发老大让统一使用Ubuntu来开发,安装了Ubuntu 15.10之后,设置了最小化桌面的快捷键为Super+D(在Systm Settings/系统设置—>Keyb ...
- Css3 javascript 写的分类
不兼容IE10以下的浏览器 <!DOCTYPE html> <html> <head> <meta charset=utf-> <title> ...
- R for installing package 'omg'
The time i have tried to install the package named 'PODBC' and it worked. But now i meet a problem ...
- 在选择“Add Library Package reference”时提示:无法加在一个或多个请求的类型,有关更多信息,请检索LoaderException属性
- Codeforces739E Gosha is hunting
题意:现在有n个精灵,两种精灵球各m1和m2个,每个精灵单独使用第一种精灵球有pi的概率被捕获,单独使用第二种精灵球有ui的概率被捕获,同时使用有1-(1-pi)*(1-ui)的概率被捕获.一种精灵球 ...
- 转:窗口启用/禁用功能函数EnableWindow的使用
在非MFC环境中如何使控件或者窗口禁用呢?起初是想通过发送消息来实现,但找来找去都木有找到控件禁用的消息(也是是博主木有找到的缘故),所以只能另辟蹊径,使用 EnableWindow这个函数, 该函数 ...
- hive 三种启动方式及用途,关注通过jdbc连接的启动
http://blog.csdn.net/a221133/article/details/6734746
- Intel hex 文件格式解密
Intel hex 文件常用来保存单片机或其他处理器的目标程序代码.它保存物理程序存储区中的目标代码映象.一般的编程器都支持这种格式. Intel hex 文件全部由可打印的ASCII字符组成(可以用 ...
- Kcptun 是一个非常简单和快速的,基于KCP 协议的UDP 隧道,它可以将TCP 流转换为KCP+UDP 流
本博客曾经发布了通过 Finalspeed 加速 Shadowsocks 的教程,大家普遍反映能达到一个非常不错的速度.Finalspeed 虽好,就是内存占用稍高,不适合服务器内存本来就小的用户:而 ...
- DELPHI7如何调用带参数的JAVA WebService
对方提供的WebService地址是http://192.168.1.6:8080/test/pic?XH=XX用DELPHI如何调呢 ------解决方案--------------------通过 ...