多部分/表单数据示例
我想知道是否有人可以与我分享一个 multipart/form-data 的示例,其中包含:
- 一些表单参数
- 多个文件
I am wondering if anyone can share with me an example of multipart/form-data that contains:
- Some form parameters
- Multiple files
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑:我在以下位置保留了类似但更深入的答案:https://stackoverflow.com /a/28380690/895245
要准确了解正在发生的情况,请使用
nc -l
或 ECHO 服务器以及用户代理(例如浏览器或 cURL)。将表单保存到
.html
文件:创建要上传的文件:
运行:
在浏览器上打开 HTML,选择文件,然后单击“提交”并检查终端。
nc
打印收到的请求。 Firefox 发送:或者,cURL 应该发送与浏览器表单相同的 POST 请求:
您可以使用以下方法进行多个测试:
EDIT: I am maintaining a similar, but more in-depth answer at: https://stackoverflow.com/a/28380690/895245
To see exactly what is happening, use
nc -l
or an ECHO server and a user agent like a browser or cURL.Save the form to an
.html
file:Create files to upload:
Run:
Open the HTML on your browser, select the files and click on submit and check the terminal.
nc
prints the request received. Firefox sent:Aternativelly, cURL should send the same POST request as your a browser form:
You can do multiple tests with:
非常感谢@Ciro Santilli 的回答!我发现他对边界的选择相当“不高兴”,因为所有连字符:事实上,正如 @Fake Name 评论的那样,当您在请求内使用边界时,它前面还有两个连字符:
示例:
I发现在此 w3.org 页面可以将 multipart/mixed 标头封装在 multipart/form-data 中,只需在 multipart/mixed 中选择另一个边界字符串并使用该边界字符串来封装数据。最后,您必须“关闭”FILO 中使用的所有边界才能关闭 POST 请求(例如:
查看上面的链接。
Many thanks to @Ciro Santilli answer! I found that his choice for boundary is quite "unhappy" because all of thoose hyphens: in fact, as @Fake Name commented, when you are using your boundary inside request it comes with two more hyphens on front:
Example:
I found on this w3.org page that is possible to incapsulate multipart/mixed header in a multipart/form-data, simply choosing another boundary string inside multipart/mixed and using that one to incapsulate data. At the end, you must "close" all boundary used in FILO order to close the POST request (like:
Take a look at the link above.
有一个多部分数据的示例(Angular):
2.trip-upload.component.ts
当浏览器了解您在表单中用于 HTTP POST 请求的 enctype 时,用户代理会向服务器配置名称/值对列表。根据传输数据的类型和数量,其中一种方法将比另一种更有效:
There is an example of the multipart data (Angular):
2.trip-upload.component.ts
When Browser understand which enctype you use in your form for HTTP POST requests, user-agent configure list of name/value pairs to the server. Depending on the type and amount of data being transmitted, one of the methods will be more efficient than the other: