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 ...
随机推荐
- thinkphp 配置
ThinkPHP框架中所有配置文件的定义格式均采用返回PHP数组的方式,格式为: //项目配置文件 return array( 'DEFAULT_MODULE' => 'Index', //默认 ...
- Python自动化运维之9、模块之sys、os、hashlib、random、time&datetime、logging、subprocess
python模块 用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需 ...
- app 测试点
以下所有测试最后必须在真机上完整的执行1.安装.卸载测试 在真机上的以及通过91等第三方的安装与卸载 安装在手机上还是sd卡上 2.启动app测试3.升级测试 数字签名.升级覆盖安装.下载后手动覆盖安 ...
- Sicily 1021. Couples
题目地址:1021. Couples 思路: 想清楚了这道题其实很简单.利用夫妻出现的位置作为下标,并设为同一值,第一对夫妻值为1,第二对为2,以此类推,存储完毕即可进入下一步. 利用栈这个数据结构: ...
- Cracking the coding interview--Q1.3
原文 Given two strings, write a method to decide if one is a permutation of the other. 译文 给你两个字符串,写一个方 ...
- QT文档如何使用
http://blog.csdn.net/chenlong12580/article/details/7389588
- ASCII、Unicode、GBK和UTF-8字符编码的区别联系[转]
http://dengo.org/archives/901 这是我看过的最好的一篇讲述编码的文章 很久很久以前,有一群人,他们决定用8个可以开合的晶体管来组合成不同的状态,以表示世界上的万物.他们看到 ...
- HDU_2016——数据的交换输出
Problem Description 输入n(n<100)个数,找出其中最小的数,将它与最前面的数交换后输出这些数. Input 输入数据有多组,每组占一行,每行的开始是一个整数n,表示这 ...
- [LeetCode] 148. Sort List 解题思路
Sort a linked list in O(n log n) time using constant space complexity. 问题:对一个单列表排序,要求时间复杂度为 O(n*logn ...
- [LeetCode] 21. Merge Two Sorted Lists 解题思路
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...