一个< input>中包含HTTP multipart/form-data多个文件. [英] HTTP multipart/form-data multiple files in one <input>

查看:126
本文介绍了一个< input>中包含HTTP multipart/form-data多个文件.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 W3c ,多个文件在<input>字段中选择的内容,应按多部分/混合"类型发送,并带有单独的边界字符串和仅一个名称"参数(只要名称在表单中应唯一)即可.

According to W3c, multiple files selected in a <input> field, should be send by "multipart/mixed" type with separate boundary string and only one "name" parameter (as long, as the name should be unique in the form).

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

Writing POST data processing, I noticed that the major browsers send such multiple files as if they origins from different <input> elements, but with the same name. I.e. Instead of:

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

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

...they send something like:

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

问题:

我应该如何处理POST数据?是否有浏览器会以多部分/混合"形式发送多个文件,或者不需要处理这种情况,我应该简化代码吗?

The question:

How I should process the POST data? Are there browsers that will send multiple files as a "multipart/mixed" or handling such case is not needed and I should simplify my code?

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

Notice: I am writing framework for handling HTTP, so using other libraries and frameworks is not an option.

推荐答案

我已确认您找到的内容. 我测试了Firefox和Chromium,这就是我得到的:

I have confirmed what you found. I tested Firefox and Chromium, and this is what I get:

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

经过调查,我发现 W3c信息您提供的 基于RFC2388, RFC7578 已使它过时.

After an investigation, I found that the W3c information you provided is based on RFC2388, which is already made obsolete by RFC7578.

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

According to RFC7578 Section 4.3 (with my emphasis):

[RFC2388]建议使用嵌套的多部分/混合"部分来传输单个表单字段的多个文件. 已弃用此用法.

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

To match widely deployed implementations, multiple files MUST be sent by supplying each file in a separate part but all with the same "name" parameter.

所以,你的问题:

我应该如何处理POST数据?

How I should process the POST data?

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

My recommendation is ignore that W3c info and follow RFC7578.

是否有浏览器会以多部分/混合"形式发送多个文件,或者不需要处理这种情况,我应该简化代码吗?

Are there browsers that will send multiple files as a "multipart/mixed" or handling such case is not needed and I should simplify my code?

很老的浏览器可能会使用多部分/混合",但无论如何都已弃用,因此无需处理这种情况.

Very old browsers may use "multipart/mixed" but the usage is deprecated anyway, so no need to handle such case.

我的建议:您绝对应该简化代码.

My recommendation: you should definitely simplify your code.

这篇关于一个&lt; input&gt;中包含HTTP multipart/form-data多个文件.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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