如果没有给出字符集,应该使用什么默认编码来解码 multipart/form-data? RFC2388 指出:
4.5 Charset of text in form data
Each part of a multipart/form-data is supposed to have a content- type. In the case where a field element is text, the charset parameter for the text indicates the character encoding used.
For example, a form with a text field in which a user typed 'Joe owes <eu>100' where <eu> is the Euro symbol might have form data returned as:
--AaB03x content-disposition: form-data; name="field1" content-type: text/plain;charset=windows-1250 content-transfer-encoding: quoted-printable>> Joe owes =80100. --AaB03x
在我的例子中,字符集没有设置,我不知道如何解码该文本/纯文本部分中的数据。由于我不想强制执行非标准行为,所以我想问一下在这种情况下预期的行为是什么。 RFC 似乎没有对此进行解释,所以我有点迷路了。
谢谢!
最佳答案
这显然在 HTML5 中发生了变化(参见 http://dev.w3.org/html5/spec-preview/constraints.html#multipart-form-data)。
The parts of the generated multipart/form-data resource that correspond to non-file fields must not have a Content-Type header specified.
那么字符集是在哪里指定的呢?据我所知,从编码算法来看,唯一的位置是在名为 _charset_ 的表单数据集条目中。
如果您的表单没有名为 _charset_ 的隐藏输入,会发生什么?我已经在 Chrome 28 中对此进行了测试,发送了一个以 UTF-8 编码的表单和一个以 ISO-8859-1 编码的表单,并检查了发送的 header 和有效负载,但我没有看到任何地方给出的字符集(即使文本编码确实发生了变化).如果我在表单中包含一个空的 _charset_ 字段,Chrome 会使用正确的字符集类型填充它。我想任何服务器端代码都必须查找那个 _charset_ 字段才能弄清楚?
我在编写使用 FormData 对象的 XMLHttpRequest.send 的 Chrome 扩展时遇到了这个问题,always gets encoded in UTF-8 no matter what the source document encoding is .
Let the request entity body be the result of running the multipart/form-data encoding algorithm with data as form data set and with utf-8 as the explicit character encoding.
Let mime type be the concatenation of "multipart/form-data;", a U+0020 SPACE character, "boundary=", and the multipart/form-data boundary string generated by the multipart/form-data encoding algorithm.
正如我之前发现的,charset=utf-8 没有在 POST 请求中的任何地方指定,除非您在表单中包含一个空的 _charset_ 字段,在这种情况下,它会自动填充 "utf-8”。
这是我对事物状态的理解。欢迎对我的假设进行任何更正!
关于http - multipart/form-data,字段的默认字符集是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4085449/