Response.AddHeader实现下载,解决乱码问题

本文介绍了在ASP.NET中使用Response.AddHeader实现下载,同时解决了文件名乱码问题。详细讲解了分块下载、HttpResponse.TransmitFile方法、WriteFile方法以及流方式下载,并探讨了HTTP响应头和实体头的相关设置,确保下载过程的正确性和效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近处理下载,顺便整理一下代码以便以后使用,直接上代码:

1、分块下载

            try
            {
                var templatePath = "/Download/test.xls";

                string filename = Server.MapPath(templatePath);

                System.IO.FileInfo fileInfo = new System.IO.FileInfo(filename);
                if (fileInfo.Exists == true)
                {
                    const long chunkSize = 204800;// 每次读取200K,缓解服务器的压力
                    byte[] buffer = new byte[chunkSize];

                    Response.Clear();
                    System.IO.FileStream iStream = System.IO.File.OpenRead(filename);
                    long dataLengthToRead = iStream.Length;//获取下载的文件总大小
                    Response.ContentType = "application/vnd.ms-excel";
                    Response.AddHeader("Content-Disposition", "attachment;filename="+ HttpUtility.UrlEncode("XXXXXX模板.xls"));
                    Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                    Response.AddHeader("Content-Transfer-Encoding", "binary");
                    Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
                    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();
                }
            }
            catch (Exception)
            {
                Response.Write("服务器异常,请稍后再试!");
            }

注:

1、分块读取到内存然后下载,不怕内存溢出。

2、HttpUtility.UrlEncode对文件名进行编码,可防止下载时乱码


以下几个来自网络,传送门

2、HttpResponse.TransmitFile 方法

	Response.ContentType = "application/x-zip-compressed";
        Response.AddHeader("Content-Disposition", "attachment;filename=z.zip");
        string filename = Server.MapPath("DownLoad/z.zip");
        Response.TransmitFile(filename);

注:msdn解释为:将指定的文件直接写入 HTTP 响应输出流,而不在内存中缓冲该文件。明显不适合大文件

3、WriteFile方法

        string fileName ="test.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();

4、流方式下载

        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();


HTTP响应头和实体头

以下内容来自维基百科

响应字段

Field name Description Example Status
Access-Control-Allow-Origin 指定哪些网站可参与到 跨来源资源共享 过程中  Access-Control-Allow-Origin: * Provisional
Accept-Patch[23] Specifies which patch document formats this server supports Accept-Patch: text/example;charset=utf-8 Permanent
Accept-Ranges 这个服务器支持哪些种类的部分内容范围  Accept-Ranges: bytes Permanent
Age 这个对象在 代理缓存 中存在的时间,以秒为单位  Age: 12 Permanent
Allow 对于特定资源有效的动作。针对405不允许该方法( 405 Method not allowed )而使用  Allow: GET, HEAD Permanent
Cache-Control 向从服务器直到客户端在内的所有缓存机制告知,它们是否可以缓存这个对象。其单位为秒  Cache-Control: max-age=3600 Permanent
Connection 针对该连接所预期的选项

[3]

Connection: close Permanent
Content-Disposition[24] An opportunity to raise a "File Download" dialogue box for a known MIME type with binary format or suggest a filename for dynamic content. Quotes are necessary with special characters. Content-Disposition: attachment; filename="fname.ext" Permanent
Content-Encoding 在数据上使用的编码类型。参考 超文本传输协议压缩 。  Content-Encoding: gzip Permanent
Content-Language 内容所使用的语言

[25]

Content-Language: da Permanent
Content-Length 回应消息体的长度,以 字节 (8位为一字节)为单位  Content-Length: 348 Permanent
Content-Location 所返回的数据的一个候选位置  Content-Location: /index.htm Permanent
Content-MD5 回应内容的二进制 MD5 散列,以 Base64 方式编码  Content-MD5: Q2hlY2sgSW50ZWdyaXR5IQ== Obsolete[26]
Content-Range 这条部分消息是属于某条完整消息的哪个部分  Content-Range: bytes 21010-47021/47022 Permanent
Content-Type 当前内容的MIME类型  Content-Type: text/html; charset=utf-8 Permanent
Date 此条消息被发送时的日期和时间(按照 RFC 7231 中定义的“超文本传输协议日期”格式来表示)  Date: Tue, 15 Nov 1994 08:12:31 GMT Permanent
ETag 对于某个资源的某个特定版本的一个标识符,通常是一个 消息散列  ETag: "737060cd8c284d8af7ad3082f209582d" Permanent
Expires 指定一个日期/时间,超过该时间则认为此回应已经过期  Expires: Thu, 01 Dec 1994 16:00:00 GMT Permanent: standard
Last-Modified 所请求的对象的最后修改日期(按照 RFC 7231 中定义的“超文本传输协议日期”格式来表示)  Last-Modified: Tue, 15 Nov 1994 12:45:26 GMT Permanent
Link 用来表达与另一个资源之间的类型关系,此处所说的类型关系是在 RFC 5988 中定义的  Link: </feed>; rel="alternate"[27] Permanent
Location 用来 进行 重定向 ,或者在创建了某个新资源时使用。  Location: http://www.w3.org/pub/WWW/People.html Permanent
P3P This field is supposed to set P3P policy, in the form of P3P:CP="your_compact_policy". However, P3P did not take off,[28] most browsers have never fully implemented it, a lot of websites set this field with fake policy text, that was enough to fool browsers the existence of P3P policy and grant permissions for third party cookies. P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info." Permanent
Pragma 与具体的实现相关,这些字段可能在请求/回应链中的任何时候产生多种效果。  Pragma: no-cache Permanent
Proxy-Authenticate 要求在访问代理时提供身份认证信息。  Proxy-Authenticate: Basic Permanent
Public-Key-Pins[29] 用于缓解 中间人攻击 ,声明网站的认证用的 传输 层安全协议 证书的散列值  Public-Key-Pins: max-age=2592000; pin-sha256="E9CZ9INDbd+2eRQozYqqbQ2yXLVKB9+xcprMF+44U1g="; Permanent
Refresh Used in redirection, or when a new resource has been created. This refresh redirects after 5 seconds. Refresh: 5; url=http://www.w3.org/pub/WWW/People.html Proprietary and non-standard: a header extension introduced by Netscape and supported by most web browsers.
Retry-After 如果某个实体临时不可用,则,此协议头用来告知客户端日后重试。其值可以是一个特定的时间段(以秒为单位)或一个超文本传输协议日期。 [30]
  • Example 1: Retry-After: 120
  • Example 2: Retry-After: Fri, 07 Nov 2014 23:59:59 GMT

Permanent

Server 服务器的名字  Server: Apache/2.4.1 (Unix) Permanent
HTTP cookie Set-Cookie: UserID=JohnDoe; Max-Age=3600; Version=1 Permanent: standard
Status 通用网关接口 协议头字段,用来说明当前这个超文本传输协议回应的 状态 。普通的超文本传输协议回应,会使用单独的“状态行”("Status-Line")作为替代,这一点是在 RFC 7230 中定义的。 

[31]

Status: 200 OK Not listed as a registered field name
Strict-Transport-Security A HSTS Policy informing the HTTP client how long to cache the HTTPS only policy and whether this applies to subdomains. Strict-Transport-Security: max-age=16070400; includeSubDomains Permanent: standard
Trailer The Trailer general field value indicates that the given set of header fields is present in the trailer of a message encoded with chunked transfer coding. Trailer: Max-Forwards Permanent
Transfer-Encoding 用来将实体安全地传输给用户的编码形式。 当前定义 的方法 包括: 分 块 、压缩(compress)、缩小(deflate)、压缩(gzip)、实体(identity)。  Transfer-Encoding: chunked Permanent
Upgrade 要求客户端升级到另一个协议。  Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11 Permanent
Vary 告知下游的代理服务器,应当如何对未来的请求协议头进行匹配,以决定是否可使用已缓存的回应内容而不是重新从原始服务器请求新的内容。  Vary: * Permanent
Via 告知代理服务器的客户端,当前回应是通过什么途径发送的。  Via: 1.0 fred, 1.1 example.com (Apache/1.1) Permanent
Warning 一般性的警告,告知在实体内容体中可能存在错误。  Warning: 199 Miscellaneous warning Permanent
WWW-Authenticate 表明在请求获取这个实体时应当使用的认证模式。  WWW-Authenticate: Basic Permanent
X-Frame-Options[32] Clickjacking protection: deny - no rendering within a frame, sameorigin - no rendering if origin mismatch, allow-from - allow from specified location, allowall - non-standard, allow from any location X-Frame-Options: deny Obsolete[33]

常见的非标准回应字段

字段名  说明 示例
X-XSS-Protection[34]跨站脚本攻击 (XSS)过滤器 X-XSS-Protection: 1; mode=block
Content-Security-PolicyX-Content-Security-PolicyX-WebKit-CSP[35]内容安全策略定义。 X-WebKit-CSP: default-src 'self'
X-Content-Type-Options[36]The only defined value, "nosniff", prevents Internet Explorer from MIME-sniffing a response away from the declared content-type. This also applies to Google Chrome, when downloading extensions.[37]X-Content-Type-Options: nosniff
X-Powered-By[38]表明用于支持当前网页应用程序的技术(例如PHP)(版本号细节通常放置在 X-Runtime 或 X-Version 中) X-Powered-By: PHP/5.4.0
X-UA-Compatible[39]Recommends the preferred rendering engine (often a backward-compatibility mode) to use to display the content. Also used to activate Chrome Frame in Internet Explorer.X-UA-Compatible: IE=EmulateIE7

X-UA-Compatible: IE=edge
X-UA-Compatible: Chrome=1

X-Content-Duration[40]Provide the duration of the audio or video in seconds; only supported by Gecko browsersX-Content-Duration: 42.666

请求字段

协议头字段名  说明 示例 状态
Accept能够接受的回应内容类型(Content-Types)。参见内容协商。Accept: text/plainPermanent
Accept-Charset能够接受的字符集Accept-Charset: utf-8Permanent
Accept-Encoding能够接受的编码方式列表。参考 超文本传输协议压缩 。Accept-Encoding: gzip, deflatePermanent
Accept-Language能够接受的回应内容的自然语言列表。参考 内容协商 。 Accept-Language: en-USPermanent
Accept-Datetime能够接受的按照时间来表示的版本 Accept-Datetime: Thu, 31 May 2007 20:35:00 GMTProvisional
Authorization用于超文本传输协议的认证的认证信息 Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==Permanent
Cache-Control用来指定在这次的请求/回复链中的所有缓存机制 都必须 遵守的指令 Cache-Control: no-cachePermanent
Connection该浏览器想要优先使用的连接类型

[3]

Connection: keep-alive

Connection: Upgrade

Permanent
Cookie之前由服务器通过 Set- Cookie (下文详述)发送的一个 超文本传输协议CookieCookie: $Version=1; Skin=new;Permanent: standard
Content-Length以 八位字节数组 (8位的字节)表示的请求体的长度 Content-Length: 348Permanent
Content-MD5请求体的内容的二进制 MD5 散列值,以 Base64 编码的结果 Content-MD5: Q2hlY2sgSW50ZWdyaXR5IQ==Obsolete[4]
Content-Type请求体的 多媒体类型 (用于POST和PUT请求中) Content-Type: application/x-www-form-urlencodedPermanent
Date发送该消息的日期和时间(按照 RFC 7231 中定义的"超文本传输协议日期"格式来发送) Date: Tue, 15 Nov 1994 08:12:31 GMTPermanent
Expect表明客户端要求服务器做出特定的行为 Expect: 100-continuePermanent
From发起此请求的用户的邮件地址 From: user@example.comPermanent
Host服务器的域名(用于 虚拟 主机 ),以及服务器所监听的 传输控制协议端口 号。如果所请求的端口是对应的服务的标准端口,则 端口 号可被省略。 

[5] 自超文件传输协议版本1.1(HTTP/1.1)开始便是必需字段。

Host: en.wikipedia.org:80

Host: en.wikipedia.org

Permanent
If-Match仅当客户端提供的实体与服务器上对应的实体相匹配时,才进行对应的操作。主要作用时,用作像 PUT 这样的方法中,仅当从用户上次更新某个资源以来,该资源未被修改的情况下,才更新该资源。 If-Match: "737060cd8c284d8af7ad3082f209582d"Permanent
If-Modified-Since允许在对应的内容未被修改的情况下返回304未修改( 304 Not Modified ) If-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMTPermanent
If-None-Match允许在对应的内容未被修改的情况下返回304未修改( 304 Not Modified ),参考 超文本传输协议 的实体标记 If-None-Match: "737060cd8c284d8af7ad3082f209582d"Permanent
If-Range如果该实体未被修改过,则向我发送我所缺少的那一个或多个部分;否则,发送整个新的实体 If-Range: "737060cd8c284d8af7ad3082f209582d"Permanent
If-Unmodified-Since仅当该实体自某个特定时间已来未被修改的情况下,才发送回应。 If-Unmodified-Since: Sat, 29 Oct 1994 19:43:31 GMTPermanent
Max-Forwards限制该消息可被代理及网关转发的次数。 Max-Forwards: 10Permanent
Origin发起一个针对 跨来源资源共享 的请求(要求服务器在回应中加入一个‘访问控制-允许来源’('Access-Control-Allow-Origin')字段)。 Origin: http://www.example-social-network.comPermanent: standard
Pragma与具体的实现相关,这些字段可能在请求/回应链中的任何时候产生 多种效果。 Pragma: no-cachePermanent
Proxy-Authorization用来向代理进行认证的认证信息。 Proxy-Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==Permanent
Range仅请求某个实体的一部分。字节偏移以0开始。参考 字节服务 。 Range: bytes=500-999Permanent
Referer [sic]表示浏览器所访问的前一个页面,正是那个页面上的某个链接将浏览器带到了当前所请求的这个页面。(“引导者”(“referrer”)这个单词,在RFC 中被拼错了,因此在大部分的软件实现中也拼错了,以至于,错误的拼法成为了标准的用法,还被当成了正确的术语) Referer: http://en.wikipedia.org/wiki/Main_PagePermanent
TE浏览器预期接受的传输编码方式:可使用回应协议头Transfer-Encoding 字段中的那些值,另外还有一个值可用,"trailers"(与" 分 块 "传输方式相关),用来表明,浏览器希望在最后一个尺寸为0的块之后还接收到一些额外的字段。 TE: trailers, deflatePermanent
User-Agent浏览器的浏览器身份标识字符串 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/21.0Permanent
Upgrade要求服务器升级到另一个协议。 Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11Permanent
Via向服务器告知,这个请求是由哪些代理发出的。 Via: 1.0 fred, 1.1 example.com (Apache/1.1)Permanent
Warning一个一般性的警告,告知,在实体内容体中可能存在错误。 Warning: 199 Miscellaneous warningPermanent

常见的非标准请求字段

字段名  说明 示例
X-Requested-With主要用于标识 Ajax 及可扩展标记语言 请求。大部分的JavaScript框架会发送这个字段,且将其值设置为 XMLHttpRequest X-Requested-With: XMLHttpRequest
DNT[6]请求某个网页应用程序停止跟踪某个用户。在火狐浏览器中,相当于X-Do-Not-Track协议头字段(自 火狐4.0 测试(Beta) 11版开始支持)。 Safari 和 Internet Explorer 9 也支持这个字段。在2011 年三月7 日,有人向互联网工程任务组提交了一个草案。  [7] 万维网协会 的跟踪保护工作组正在就此制作一项规范。[8]DNT: 1 (Do Not Track Enabled)

DNT: 0 (Do Not Track Disabled)

X-Forwarded-For[9]一个事实标准 ,用于标识某个通过超文本传输协议代理或负载均衡连接到某个网页服务器的客户端的原始互联网地址 X-Forwarded-For: client1, proxy1, proxy2

X-Forwarded-For: 129.78.138.66, 129.78.64.103

X-Forwarded-Host[10]de facto standard for identifying the original host requested by the client in the Host HTTP request header, since the host name and/or port of the reverse proxy (load balancer) may differ from the origin server handling the request.X-Forwarded-Host: en.wikipedia.org:80

X-Forwarded-Host: en.wikipedia.org

X-Forwarded-Proto[11]一个事实标准 用于标识某个超文本传输协议请求最初所使用的协议,因为,在反向代理(负载均衡)上,即使最初发往该反向代理的请求类型是安全的超文本传输协议(HTTPS),该反向代理也仍然可能会使用超文本传输协议(HTTP)来与网页服务器通信。谷歌客户端在与谷歌服务器通信时会使用该协议头的一个替代形式(X-ProxyUser-Ip)。 X-Forwarded-Proto: https
Front-End-Https[12]Non-standard header field used by Microsoft applications and load-balancersFront-End-Https: on
X-Http-Method-Override[13]请求某个网页应用程序使用该协议头字段中指定的方法(一般是PUT或DELETE)来覆盖掉在请求中所指定的方法(一般是POST)。当某个浏览器或防火墙阻止直接发送PUT 或DELETE 方法时(注意,这可能是因为软件中的某个漏洞,因而需要修复,也可能是因为某个配置选项就是如此要求的,因而不应当设法绕过),可使用这种方式。 X-HTTP-Method-Override: DELETE
X-ATT-DeviceId[14]Allows easier parsing of the MakeModel/Firmware that is usually found in the User-Agent String of AT&T DevicesX-Att-Deviceid: GT-P7320/P7320XXLPG
X-Wap-Profile[15]Links to an XML file on the Internet with a full description and details about the device currently connecting. In the example to the right is an XML file for an AT&T Samsung Galaxy S2.x-wap-profile: http://wap.samsungmobile.com/uaprof/SGH-I777.xml
Proxy-Connection[16]因为对超文本传输协议规范的误解而实现的一个协议头。因为早期超文本传输协议版本实现中的错误而出现。与标准的连接(Connection)字段的功能完全相同。 Proxy-Connection: keep-alive
X-UIDH[17][18][19]Server-side deep packet insertion of a unique ID identifying customers of Verizon Wireless; also known as "perma-cookie" or "supercookie"X-UIDH: ...
X-Csrf-Token[20]Used to prevent cross-site request forgery. Alternative header names are: X-CSRFToken[21] and X-XSRF-TOKEN[22]X-Csrf-Token: i8XNjC4b8KVok4uw5RftR38Wgp2BFwql




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值