实现方式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--下载文件的更多相关文章

  1. Asp.Net 下载文件的几种方式

    asp.net下载文件几种方式 protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新的方法 ...

  2. asp.net下载文件几种方式

    测试时我以字符流的形式下载文件,可行,前几个仅作参考 protected void Button1_Click(object sender, EventArgs e)  {  /*  微软为Respo ...

  3. Asp.net下载文件

    网站上的文件是临时文件, 浏览器下载完成, 网站需要将其删除. 下面的写法, 文件读写后没关闭, 经常删除失败. /// <summary> /// 下载服务器文件,参数一物理文件路径(含 ...

  4. asp.net 下载文件(图片、word、excel等)

    string filePath = Server.MapPath("~/excel.xlsx"); if (File.Exists(filePath)) { FileStream ...

  5. ASP.NET 下载文件方式

    protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新的方法TransmitFile来解决使 ...

  6. asp.net 下载文件几种方式

    protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新的方法TransmitFile来解决使 ...

  7. ASP.NET 下载文件并继续执行JS解决方法

    需求说明:当用户点击按钮时使当前按钮为不可用,并打开新页面,关闭新页面时,按钮变为可用.并且如果不关闭新页面,当前按钮过10秒钟自动变为可用. 包含3个页面: 一.按钮页 前台代码:当刷新后采用js进 ...

  8. asp.net下载文件方法

    /// <summary> /// 下载 /// </summary> /// <param name="url"></param> ...

  9. asp.net下载文件的几种方法

    最近做东西遇到了下载相关的问题.在这里总结一下自己处理的方法. 1.以字节流的形式向页面输出数据以下载Excel为例子. string path=Server.MapPath("文件路径&q ...

  10. 解决用ASP.NET下载文件时,文件名为乱码的问题

    关键就一句:                    string strTemp = System.Web.HttpUtility.UrlEncode(strName, System.Text.Enc ...

随机推荐

  1. PHPCMS v9构建模块

    ■补课: 1.phpcms v9帮助文件,上面会写关于二次开发的一些方法. http://v9.help.phpcms.cn/ 2.找一个后台还没安装的模块,先把代码看一边.比如dianping模块 ...

  2. AJAX快速上手

    创建XMLHttpRequest对象 xmlHttp = new XMLHttpRequest(); xmlHttp = new ActiveXObject('Microsoft.XMLHTTP'); ...

  3. ecshop有关real_ip()你发现的问题

    我这边有一个ECSHOP后台,有一个功能,在客户下订单时自动获取客户IP,用的是ECSHOP本身的real_ip函数,但这个函数存在一个问题,十个订单得到的IP,可能会有几个与CNZZ统计得到的不一样 ...

  4. Installing Mp4box in centos 6

    Installing Mp4box in centos 6   Installing Mp4box in centos 6Login to the server cd /usr/local/src/ ...

  5. Poweroff – 很好很强大的定制关机工具

    Poweroff – 很好很强大的定制关机工具 Poweroff 是一个用来管理电脑关机系统的小工具,支持定时,支持远程 作者开放源代码,有兴趣的同学可以尝试着制作一下汉化版本.   可以设定不同时间 ...

  6. java中ExecutorService接口

    一.声明 public interface ExecutorService extends Executor 位于java.util.concurrent包下 所有超级接口:Executor 所有已知 ...

  7. C#中HashTable和快速排序的用法

    题目主要是写一个程序,分析一个文本文件(英文文章)中各个词出现的频率,并且把频率最高的10个词打印出来.   自从周四拿到题目以后,发现又要用到万恶的数据结构了,不得不说这是我的短板,所有上周20号到 ...

  8. Python学习之--异常处理

    Python中的Exceptions是所有异常的基类,内置的异常类都放在了exceptions模块中,通过dir()函数可以看到这些内置的类 通过raise 语句触发异常,如 >>> ...

  9. Lua----注意事项

    前言:Lua相对一般的语言相对简单,有c基础看一遍就差不多了.一般的代码都能够看懂.但是Lua也有一些自己的特点,区别与其他语言,这里需要注意一下. 1.数组下标 在Lua中数组下标是从1开始计数的. ...

  10. rsync学习

    echo "aabb" > pswd pswd rsync -Cvaz assert.awk stat@59.151.37.17::stat/read/aabb/assert ...