在 HTTP 响应标头中使用内容处置

发布于 2024-07-24 04:56:04 字数 232 浏览 6 评论 0原文

我发现以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

寄居者 2024-07-31 04:56:05

此标头在 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.

深巷少女 2024-07-31 04:56:04

请注意,RFC 6266 取代了下面引用的 RFC。 第 7 节概述了一些相关的安全问题。

content-disposition 标头的权威是 RFC 1806RFC 2183。 人们还设计了 content-disposition hacking。 需要注意的是,content-disposition 标头不是 HTTP 1.1 标准的一部分。

HTTP 1.1 标准 (RFC 2616) 还提到了内容处置可能带来的安全副作用:

15.5 内容处置问题

RFC 1806 [35],其中经常
实施内容处置
(参见第 19.5.1 节)HTTP 中的标头是
衍生出来的,有很多很
严重的安全考虑。
内容处置不属于
HTTP 标准,但由于它是
广泛实施,我们
记录其使用和风险
实施者。 请参阅 RFC 2183 [49]
(更新 RFC 1806)了解详细信息。

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:

15.5 Content-Disposition Issues

RFC 1806 [35], from which the often
implemented Content-Disposition
(see section 19.5.1) header in HTTP is
derived, has a number of very
serious security considerations.
Content-Disposition is not part of
the HTTP standard, but since it is
widely implemented, we are
documenting its use and risks for
implementors. See RFC 2183 [49]
(which updates RFC 1806) for details.

早茶月光 2024-07-31 04:56:04

嗯,看来 Content-Disposition 标头最初是为电子邮件而不是网络创建的。 (相关 RFC 的链接。)

我猜测网络浏览器

Response.AppendHeader("content-disposition", "inline; filename=" + fileName);

在保存时 可能会做出响应,但我不确定。

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

Response.AppendHeader("content-disposition", "inline; filename=" + fileName);

when saving, but I'm not sure.

暖树树初阳… 2024-07-31 04:56:04

请参阅 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

疾风者 2024-07-31 04:56:04

对于 ASP.NET 用户,.NET 框架提供了一个类来创建内容处置标头:
System.Net.Mime.ContentDisposition< /a>

基本用法:

var cd = new System.Net.Mime.ContentDisposition();
cd.FileName = "myFile.txt";
cd.ModificationDate = DateTime.UtcNow;
cd.Size = 100;
Response.AppendHeader("content-disposition", cd.ToString());

For asp.net users, the .NET framework provides a class to create a content disposition header:
System.Net.Mime.ContentDisposition

Basic usage:

var cd = new System.Net.Mime.ContentDisposition();
cd.FileName = "myFile.txt";
cd.ModificationDate = DateTime.UtcNow;
cd.Size = 100;
Response.AppendHeader("content-disposition", cd.ToString());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文