本文翻译自:Do I need Content-Type: application/octet-stream for file download?

The HTTP standard says: HTTP标准说:

If this header [Content-Disposition: attachment] is used in a response with the application/octet-stream content-type, the implied suggestion is that the user agent should not display the response, but directly enter a `save response as...' dialog. 如果在带有application / octet-stream内容类型的响应中使用此标题[Content-Disposition:attachment],则隐含的建议是用户代理不应显示响应,而是直接输入`save response as .. '对话。

I read that as 我读到了

Content-Type: application/octet-stream
Content-Disposition: attachment

But I would have thought that Content-Type would be application/pdf , image/png , etc. 但我认为Content-Type将是application/pdfimage/png等。

Should I have Content-Type: application/octet-stream if I want browsers to download the file? 如果我想要浏览器下载文件,我应该有Content-Type: application/octet-stream吗?


#1楼

参考:https://stackoom/question/1O3Gu/我是否需要Content-Type-application-octet-stream进行文件下载


#2楼

No. 没有。

The content-type should be whatever it is known to be, if you know it. 如果您知道,内容类型应该是已知的内容。 application/octet-stream is defined as "arbitrary binary data" in RFC 2046, and there's a definite overlap here of it being appropriate for entities whose sole intended purpose is to be saved to disk, and from that point on be outside of anything "webby". application/octet-stream在RFC 2046中被定义为“任意二进制数据”,并且这里有一个明确的重叠,它适用于将其唯一预期目的保存到磁盘的实体,并且从那时起就是在任何东西之外“威比”。 Or to look at it from another direction; 或者从另一个方向看它; the only thing one can safely do with application/octet-stream is to save it to file and hope someone else knows what it's for. 应用程序/八位字节流唯一可以安全地做的就是将它保存到文件中,并希望其他人知道它的用途。

You can combine the use of Content-Disposition with other content-types, such as image/png or even text/html to indicate you want saving rather than display. 您可以将Content-Disposition与其他内容类型(例如image/png甚至text/html的使用结合起来,以指示您想要保存而不是显示。 It used to be the case that some browsers would ignore it in the case of text/html but I think this was some long time ago at this point (and I'm going to bed soon so I'm not going to start testing a whole bunch of browsers right now; maybe later). 以前有些浏览器会在text/html的情况下忽略它,但我认为这是很久以前的事情(我很快就会睡觉所以我不打算开始测试现在一大堆浏览器;也许稍后)。

RFC 2616 also mentions the possibility of extension tokens, and these days most browsers recognise inline to mean you do want the entity displayed if possible (that is, if it's a type the browser knows how to display, otherwise it's got no choice in the matter). RFC 2616还提到了扩展令牌的可能性,现在大多数浏览器都认为inline意味着你确实希望显示实体(也就是说,如果它是浏览器知道如何显示的类型,否则它就没有选择了)。 This is of course the default behaviour anyway, but it means that you can include the filename part of the header, which browsers will use (perhaps with some adjustment so file-extensions match local system norms for the content-type in question, perhaps not) as the suggestion if the user tries to save. 这当然是默认行为,但它意味着您可以包含标题的filename部分,浏览器将使用(可能需要进行一些调整,以便文件扩展名与本地系统规范相匹配,可能不是)作为用户试图保存的建议。

Hence: 因此:

Content-Type: application/octet-stream
Content-Disposition: attachment; filename="picture.png"

Means "I don't know what the hell this is. Please save it as a file, preferably named picture.png". 意思是“我不知道这到底是什么。请把它保存为文件,最好是名为picture.png”。

Content-Type: image/png
Content-Disposition: attachment; filename="picture.png"

Means "This is a PNG image. Please save it as a file, preferably named picture.png". 表示“这是PNG图像。请将其保存为文件,最好命名为picture.png”。

Content-Type: image/png
Content-Disposition: inline; filename="picture.png"

Means "This is a PNG image. Please display it unless you don't know how to display PNG images. Otherwise, or if the user chooses to save it, we recommend the name picture.png for the file you save it as". 表示“这是PNG图像。除非您不知道如何显示PNG图像,否则请显示它。否则,或者如果用户选择保存它,我们建议您将文件名为picture.png保存为”。

Of those browsers that recognise inline some would always use it, while others would use it if the user had selected "save link as" but not if they'd selected "save" while viewing (or at least IE used to be like that, it may have changed some years ago). 在那些识别inline浏览器中,有些会一直使用它,而其他浏览器会在用户选择“保存链接为”时使用它,但如果他们在查看时选择“保存”则不会使用它(或者至少IE曾经是这样的,它可能在几年前发生了变化)。

更多推荐

我是否需要Content-Type:application / octet-stream进行文件下载?