在 HTTP 响应标头中使用内容处置
我发现以下 asp.net 代码在从数据库提供文件时非常有用:
Response.AppendHeader("content-disposition", "attachment; filename=" + fileName);
这使用户可以将文件保存到他们的计算机上,然后决定如何使用它,而不是让浏览器尝试使用该文件。
使用内容处置响应标头还可以做哪些其他事情?
I have found the following asp.net code to be very useful when serving files from a database:
Response.AppendHeader("content-disposition", "attachment; filename=" + fileName);
This lets the user save the file to their computer and then decide how to use it, instead of the browser trying to use the file.
What other things can be done with the content-disposition response header?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
此标头在 RFC 2183 中定义,因此这是开始阅读的最佳位置。
允许的值是在互联网号码分配机构 (IANA) 注册的值; 他们的值注册表应该被视为权威来源。
This header is defined in RFC 2183, so that would be the best place to start reading.
Permitted values are those registered with the Internet Assigned Numbers Authority (IANA); their registry of values should be seen as the definitive source.
请注意,RFC 6266 取代了下面引用的 RFC。 第 7 节概述了一些相关的安全问题。
content-disposition 标头的权威是 RFC 1806 和 RFC 2183。 人们还设计了 content-disposition hacking。 需要注意的是,content-disposition 标头不是 HTTP 1.1 标准的一部分。
HTTP 1.1 标准 (RFC 2616) 还提到了内容处置可能带来的安全副作用:
Note that RFC 6266 supersedes the RFCs referenced below. Section 7 outlines some of the related security concerns.
The authority on the content-disposition header is RFC 1806 and RFC 2183. People have also devised content-disposition hacking. It is important to note that the content-disposition header is not part of the HTTP 1.1 standard.
The HTTP 1.1 Standard (RFC 2616) also mentions the possible security side effects of content disposition:
嗯,看来 Content-Disposition 标头最初是为电子邮件而不是网络创建的。 (相关 RFC 的链接。)
我猜测网络浏览器
在保存时 可能会做出响应,但我不确定。
Well, it seems that the Content-Disposition header was originally created for e-mail, not the web. (Link to relevant RFC.)
I'm guessing that web browsers may respond to
when saving, but I'm not sure.
请参阅 RFC 6266(超文本传输协议 (HTTP) 中内容处置标头字段的使用) https://www.rfc-editor.org/rfc/rfc6266
Refer to RFC 6266 (Use of the Content-Disposition Header Field in the Hypertext Transfer Protocol (HTTP)) https://www.rfc-editor.org/rfc/rfc6266
对于 ASP.NET 用户,.NET 框架提供了一个类来创建内容处置标头:
System.Net.Mime.ContentDisposition< /a>
基本用法:
For asp.net users, the .NET framework provides a class to create a content disposition header:
System.Net.Mime.ContentDisposition
Basic usage: