一个<input>中的HTTP multipart/form-data多个文件

joh*_*und 3 forms post http

的背景:

根据W3c,在<input>字段中选择的多个文件应该通过"multipart/mixed"类型发送,带有单独的边界字符串和只有一个"name"参数(因为名称在表单中应该是唯一的).

编写POST数据处理时,我注意到主流浏览器发送这样的多个文件,好像它们来自不同的<input>元素,但具有相同的名称.即代替:

Content-Type: multipart/form-data; boundary=AaB03x

--AaB03x
Content-Disposition: form-data; name="files"
Content-Type: multipart/mixed; boundary=BbC04y

--BbC04y
Content-Disposition: file; filename="file1.txt"
Content-Type: text/plain

... contents of file1.txt ...
--BbC04y
Content-Disposition: file; filename="file2.gif"
Content-Type: image/gif

...contents of file2.gif...
--BbC04y--
--AaB03x--
Run Code Online (Sandbox Code Playgroud)

......他们发送的内容如下:

Content-Type: multipart/form-data; boundary=AaB03x

--AaB03x
Content-Disposition: form-data; name="files"; filename="file1.txt"
Content-Type: text/plain

... contents of file1.txt ...
--BbC04y
Content-Disposition: form-data; name="files"; filename="file2.gif"
Content-Type: image/gif

...contents of file2.gif...
--AaB03x--
Run Code Online (Sandbox Code Playgroud)

问题:

我应该如何处理POST数据?是否存在将多个文件作为"multipart/mixed"发送或不需要处理此类情况的浏览器,我应该简化我的代码?

注意:我正在编写用于处理HTTP的框架,因此使用其他库和框架不是一种选择.

Rei*_*Rei 10

我已经确认你发现了什么.我测试了Firefox和Chromium,这就是我得到的:

Content-Type: multipart/form-data; boundary=---------------------------148152952621447

-----------------------------148152952621447
Content-Disposition: form-data; name="files"; filename="fileOne.txt"
Content-Type: text/plain

this is fileOne.txt
-----------------------------148152952621447
Content-Disposition: form-data; name="files"; filename="fileTwo.txt"
Content-Type: text/plain

this is fileTwo.txt
-----------------------------148152952621447--
Run Code Online (Sandbox Code Playgroud)

经过调查,我发现您提供的W3c信息 基于RFC2388,已经被RFC7578淘汰.

根据RFC7578第4.3节(我强调):

[RFC2388]建议使用嵌套的"multipart/mixed"部分传输单个表单字段的多个文件.不推荐使用此用法.

为了匹配广泛部署的实现,必须通过在单独的部分中提供每个文件来发送多个文件,但是所有文件都具有相同的"name"参数.

所以,你的问题:

我应该如何处理POST数据?

我的建议是忽略W3c信息并遵循RFC7578.

是否存在将多个文件作为"multipart/mixed"发送或不需要处理此类情况的浏览器,我应该简化我的代码?

很老的浏览器可能会使用"multipart/mixed",但无论如何都会弃用,因此无需处理此类情况.

我的建议:你一定要简化你的代码.