在 Web 应用程序中将不同的文件类型都提供为 application/octet-stream 有什么含义?

What are the implications of serving different file types all as application/octet-stream in a web application?

我的 well-answered 问题 引出了另一个问题。

我在那个原始问题中提到的 Azure 帐户不是由我们管理的。这是请求其 blob 文件时收到的 headers 示例:

HTTP/1.1 200 OK
Content-MD5: R57initOyxxq6dVKtoAx3w==
Content-Type: application/octet-stream
Date: Wed, 02 Mar 2016 14:32:35 GMT
Etag: 0x8D3180DA8EBF063
Last-Modified: Fri, 08 Jan 2016 09:25:33 GMT
Server: Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
x-ms-blob-type: BlockBlob
x-ms-lease-status: unlocked
x-ms-request-id: 19d0a689-0001-0039-2990-74a33e000000
x-ms-version: 2009-09-19
Content-Length: 263748

因此文件返回为 application/octet-stream,我理解这实际上意味着 未知文件类型 。当我在浏览器中点击 URL 时,系统会提示我下载,即使文件是图像也是如此。

最终,此 blob 存储中的文件将以两种方式使用。有些是将用于网站图像的图像。其他 'assets'(主要是 PDF)需要下载而不是在浏览器中打开。

所以我的问题是,如果我按原样保留 blob 存储,所有资产都作为 application/octet-stream 返回,在将其图像用作 Web 内容并链接到其 PDF 以供下载时是否会有任何负面影响?例如是否有行为不同的浏览器?

也就是说,我坚持把headers改成...有什么好处呢

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

...和...

Content-Type: application/pdf
Content-Disposition: attachment; filename="file.pdf"

So my question is, if I leave the blob storage as is, with all assets being returned as application/octet-stream, are there any negative implications when using its images as web content and linking to its PDFs for download? e.g. are there browsers which will behave differently?

是的,浏览器的行为不同。例如,如果您在 Azure Blob 存储中存储了一个 png 文件,内容类型为 application/octet-stream,在 Chrome 中将始终下载该文件,但在 Internet Explorer 中将显示该文件。因此,从本质上讲,浏览器将如何根据内容类型处理内容。

Ultimately the files in this blob storage will be used in 2 ways. Some are images which will be used for website imagery. Others are 'assets' (mainly PDFs) which need to be downloaded as opposed to opened in the browser.

建议您为要在浏览器中显示的内容正确设置内容类型。即使对于将要下载的内容,也请确保内容类型设置正确。对于可下载的内容,您可能需要考虑两种情况:

  1. 内容将始终被下载:如果内容将始终被下载,您可以将 blob 的内容配置 属性 设置为 attachment; filename="file.pdf" .
  2. 有时会下载内容,但有时会内联显示:在这种情况下,不要设置 blob 的内容配置 属性 并设置它的内容类型正确。所以当内容被访问时,默认情况下它会内联显示。当您需要强制下载内容时,您可以在至少具有 Read 权限的 blob 上创建一个 Shared Access Signature (SAS),并覆盖 SAS 中 blob 的 content-disposition 属性。当有人使用 SAS URL 访问 blob 时,将始终下载内容。