我需要 Content-Type: application/octet-stream 来下载文件吗? [英] Do I need Content-Type: application/octet-stream for file download?

查看:675
本文介绍了我需要 Content-Type: application/octet-stream 来下载文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTTP 标准说:

如果在响应中使用了此标头 [Content-Disposition:attachment]使用 application/octet-stream 内容类型,隐含的建议是用户代理不应该显示响应,但是直接输入将响应另存为..."对话框.

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.

我是这么读的

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

但我会认为 Content-Type 会是 application/pdfimage/png

But I would have thought that Content-Type would be application/pdf, image/png, etc.

如果我想让浏览器下载文件,我应该有 Content-Type: application/octet-stream 吗?

Should I have Content-Type: application/octet-stream if I want browsers to download the file?

推荐答案

没有

内容类型应该是任何已知的,如果你知道的话.application/octet-stream 在 RFC 2046 中被定义为任意二进制数据",这里有一个明确的重叠,它适用于其唯一目的是保存到磁盘的实体,并且从那个点在任何韦比"之外.或者从另一个方向看;唯一可以安全地使用 application/octet-stream 的事情就是将其保存到文件中,并希望其他人知道它的用途.

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". 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.

您可以结合使用 Content-Disposition 和其他内容类型,例如 image/png 甚至 text/html表示您要保存而不是显示.过去,在 text/html 的情况下,某些浏览器会忽略它,但我认为这是很久以前的事了(我很快就要睡觉了,所以我我现在不打算开始测试一大堆浏览器;也许以后).

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. 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).

RFC 2616 还提到了扩展令牌的可能性,现在大多数浏览器都认为 inline 意味着您确实希望尽可能显示实体(也就是说,如果它是浏览器知道如何显示的类型)显示,否则在这件事上别无选择).这当然是默认行为,但这意味着您可以包含标题的 filename 部分,浏览器将使用该部分(可能进行一些调整,使文件扩展名与内容的本地系统规范相匹配- 输入有问题,也许不是)作为用户尝试保存的建议.

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). 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.

因此:

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

意思是我不知道这是什么鬼东西.请将它保存为一个文件,最好命名为picture.png".

Means "I don't know what the hell this is. Please save it as a file, preferably named picture.png".

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

表示这是一个 PNG 图像.请将其另存为文件,最好命名为 picture.png".

Means "This is a PNG image. Please save it as a file, preferably named picture.png".

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

表示这是一个PNG图片.请显示它,除非您不知道如何显示PNG图片.否则,或者如果用户选择保存它,我们建议您保存的文件名称为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".

在那些识别 inline 的浏览器中,有些浏览器会一直使用它,而其他浏览器会在用户选择将链接另存为"时使用它,但如果他们在查看时选择了保存"则不会使用(或者至少 IE 曾经是这样,它可能在几年前已经改变了).

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).

这篇关于我需要 Content-Type: application/octet-stream 来下载文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆