// 方法一:TransmitFile实现下载
protected void Button1_Click(object sender, EventArgs e)
{
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename=z.zip");
string filename = Server.MapPath("DownLoad/z.zip");
Response.TransmitFile(filename);

ontentType 属性指定响应的 HTTP 内容类型。如果未指定 ContentType,默认为 text/HTML。

语法 Response.ContentType [= ContentType ] 参数 ContentType 描述内容类型的字符串。该字符串通常被格式化为类型/子类型,其中类型是常规内容范畴而子类为特定内容类型。

 

       // 方法二:WriteFile实现下载
protected void Button2_Click(object sender, EventArgs e)
{
string fileName = "asd.txt";//客户端保存的文件名
string filePath = Server.MapPath("DownLoad/aaa.txt");//路径 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.WriteFile(fileInfo.FullName);
Response.Flush();
Response.End();
}

 response.write的意思是返回一个消息给其它页面也可以,当前页面也行的。

        // 方法三:WriteFile分块下载
protected void Button3_Click(object sender, EventArgs e)
{
string fileName = "aaa.txt";//客户端保存的文件名
string filePath = Server.MapPath("DownLoad/aaa.txt");//路径 System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath); if (fileInfo.Exists == true)
{
const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
byte[] buffer = new byte[ChunkSize]; Response.Clear();
System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
long dataLengthToRead = iStream.Length;//获取下载的文件总大小
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
while (dataLengthToRead > 0 && Response.IsClientConnected)
{
int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
Response.OutputStream.Write(buffer, 0, lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
Response.Close();
}
}

  

       // 方法四:流方式下载
protected void Button4_Click(object sender, EventArgs e)
{
string fileName = "aaa.txt";//客户端保存的文件名
string filePath = Server.MapPath("DownLoad/aaa.txt");//路径 //以字符流的形式下载文件
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.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 浏览器下载文件的四种方式的更多相关文章

  1. C#从服务器下载文件的四种方式

    //方法一:TransmitFile实现下载 string fileName = "ss.docx"; //客户端预设的文件名,导出时可修改  string filePath = ...

  2. linux服务器之间传输文件的四种方式

    linux文件传输在内网渗透中至关重要,所以我在此总结一下几种Linux服务器之间传输文件的四种方式 1. scp [优点]简单方便,安全可靠:支持限速参数[缺点]不支持排除目录[用法]scp就是se ...

  3. java读取XML文件的四种方式

    java读取XML文件的四种方式 Xml代码 <?xml version="1.0" encoding="GB2312"?> <RESULT& ...

  4. 从后端接口下载文件的2种方式:get方式、post方式

    从后端接口下载文件的2种方式 一.get方式 直接使用: location.href='http://www.xxx.com/getFile?params1=xxx&params2=xxxx' ...

  5. 【文件下载】Java下载文件的几种方式

    [文件下载]Java下载文件的几种方式  摘自:https://www.cnblogs.com/sunny3096/p/8204291.html 1.以流的方式下载. public HttpServl ...

  6. linux创建文件的四种方式(其实是两种,强行4种)

    linux创建文件的四种方式: 1.vi newfilename->i->编辑文件->ESC->:wq! 2.touch newfilename 3.cp sourcePath ...

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

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

  8. C# 下载文件的四种方法

    C# 文件下载四方法 - CSDN论坛 - CSDN.NET using System; using System.Data; using System.Configuration; using Sy ...

  9. 解析xml文件的四种方式

    什么是 XML? XML 指可扩展标记语言(EXtensible Markup Language) XML 是一种标记语言,很类似 HTML XML 的设计宗旨是传输数据,而非显示数据 XML 标签没 ...

随机推荐

  1. javascript 和 jquery插件开发

    window.onload 页面加载完成后执行 window.onload = function () { function dom(id) { return document.getElementB ...

  2. js未分类

    jQuery设置透明 1.直接.fadeIn 淡入 .fadeOut 淡出 .fadeTo(时间,"透明度") 2.addClass只能控制淡入和淡出,不能控制其速度. filte ...

  3. asp.net正则表达式

    导入引用命名空间:using System.Text.RegularExpressions //Regex类,常用方法: //摘要:1.IsMatch(String);2.IsMatch(String ...

  4. Azure Paas SQL 修改用户名密码的相关问题

    现总结如下,供您参考: 1)  如何单独修改每个数据库的密码? 在portal中,我们提供了一个最高权限的,可管理服务器下所有数据库的服务器用户 跟密码,但在实际使用中,由于权限过大,会有潜在的安全隐 ...

  5. linux CPU占用率高(转)

    来自:http://www.cnitblog.com/houcy/archive/2012/11/28/86801.html 1.用top命令查看哪个进程占用CPU高 gateway网关进程14094 ...

  6. 用Gearman分发PHP应用程序的工作负载

    文章来源:PHP开发学习门户 地址:http://www.phpthinking.com/archives/518 虽然一个 Web 应用程序的大部分内容都与表示有关,但它的价值与竞争优势却可能体如今 ...

  7. CentOS7 nginx+tomcat实现代理访问java web项目让项目支持jsp和php

    如果安装了Tomcat查看版本方法如下:注意:先进到tomcat的bin目录下(cd /tomcat目录/bin),在执行./version.sh [root@izbp bin]# ./version ...

  8. Spring Remoting: Hessian

  9. POJ--2406Power Strings+KMP求字符串最小周期

    题目链接:点击进入 事实上就是KMP算法next数组的简单应用.假设我们设这个字符串的最小周期为x 长度为len,那么由next数组的意义,我们知道len-next[len]的值就会等于x.这就是这个 ...

  10. 完完全全彻底删除VMware_Workstation

    vmware-workstation,卸载不干净.bat脚本自动处理dll.注册的表垃圾 Download https://pan.baidu.com/s/1dmrOs3rkR8cX5b0vTUOxH ...