@content_type in paperclip gem returns 不同的值

@content_type in paperclip gem returns different value

在windows中,我得到:

@content_type="application/octet-stream"

完整跟踪:

"attachments_attributes"=>{"0"=>{"attachment"=>#<ActionDispatch::Http::UploadedFile:0x007f7bb52becc8 @original_filename="ms_document.doc", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; name=\"post[attachments_attributes][0][attachment]\"; filename=\"doc\"\r\nContent-Type: application/octet-stream\r\n", @tempfile=#<Tempfile:/tmp/RackMultipart20160108-8859-x51qhj>>}}}

在 Linux 我得到:

@content_type="application/msword"

完整跟踪:

"attachments_attributes"=>{"0"=>{"attachment"=>#<ActionDispatch::Http::UploadedFile:0x007f7bb5d585d0 @original_filename="ms_document.doc", @content_type="application/msword", @headers="Content-Disposition: form-data; name=\"post[attachments_attributes][0][attachment]\"; filename=\"doc.doc\"\r\nContent-Type: application/msword\r\n", @tempfile=#<Tempfile:/tmp/RackMultipart20160108-8859-9m1xgv>>}}}

问题是,为什么我在上传相同的文件时收到不同的@content_type,但只收到不同的OS。

我正在使用:

  1. rails 3
  2. 回形针'4.3.2'

HTTP 规范here 规定

When an entity-body is included with a message, the data type of that body is determined via the header fields Content-Type and Content- Encoding. These define a two-layer, ordered encoding model:

entity-body := Content-Encoding( Content-Type( data ) ) Content-Type specifies the media type of the underlying data. Content-Encoding may be used to indicate any additional content codings applied to the data, usually for the purpose of data compression, that are a property of the requested resource. There is no default encoding.

Any HTTP/1.1 message containing an entity-body SHOULD include a Content-Type header field defining the media type of that body. If and only if the media type is not given by a Content-Type field, the recipient MAY attempt to guess the media type via inspection of its content and/or the name extension(s) of the URI used to identify the resource. If the media type remains unknown, the recipient SHOULD treat it as type "application/octet-stream".

在你的情况下,这可能发生了,因为文件所在的服务器 sent/uploaded 无法确定文件的类型,因为浏览器可能没有设置 Content-Type 在发送文件时在 HTTP 消息中。

这取决于您使用的浏览器和 OS。 windows 上的浏览​​器可能未设置内容类型。