多部分/表单数据的示例 [英] Example of multipart/form-data

查看:155
本文介绍了多部分/表单数据的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 一些表单参数

    li>
  2. 多个档案


解决方案

strong>:我正在维护一个类似但更深入的答案: https://stackoverflow.com/a/28380690/895245



要准确查看发生了什么,请使用 nc -l <​​/ code>或ECHO服务器和用户代理如浏览器或cURL。



将表单保存为 .html 文件:

 < form action =http:// localhost:8000method =postenctype =multipart / form-data> 
< p>< input type =textname =textvalue =text default>
< p>< input type =filename =file1>
< p>< input type =filename =file2>
< p><按钮类型=提交>提交< /按钮>
< / form>

创建要上传的文件:

  echo'a.txt。的内容> a.txt 
echo'<!DOCTYPE html>< title> a.html的内容。< / title>> a.html

运行:

  nc -l localhost 8000 

在您的浏览器中打开HTML,选择文件并点击提交并检查终端。

nc 会打印收到的请求。 Firefox发送:

$ p $ POST / HTTP / 1.1
主机:localhost:8000
用户代理:Mozilla /5.0(X11; Ubuntu; Linux i686; rv:29.0)Gecko / 20100101 Firefox / 29.0
接受:text / html,application / xhtml + xml,application / xml; q = 0.9,* / *; q = 0.8
Accept-Language:en-US,en; q = 0.5
Accept-Encoding:gzip,deflate
Cookie:__atuvc = 34%7C7;永久= 0; _gitlab_session = 226ad8a0be43681acf38c2fab9497240; __profilin = P%3DT; request_method = GET
连接:keep-alive
Content-Type:multipart / form-data; boundary = --------------------------- 9051914041544843365972754266
Content-Length:554

--- -------------------------- 9051914041544843365972754266
Content-Disposition:form-data; name =text

文本默认
----------------------------- 9051914041544843365972754266
Content-Disposition:form-data; NAME = file1的;文件名=a.txt
内容类型:文本/纯文本

a.txt的内容。

----------------------------- 9051914041544843365972754266
Content-Disposition:form-data; NAME = file2的;文件名=a.html
内容类型:text / html

<!DOCTYPE html>< title> a.html的内容< / title>

----------------------------- 9051914041544843365972754266--

另外,cURL应该发送与浏览器表单相同的POST请求:

  nc -l localhost 8000 
curl -Ftext = default-Ffile1=@a.html-Ffile1=@a.txtlocalhost:8000

您可以使用以下方式进行多项测试:

  while true;做printf''| nc -l localhost 8000;完成


I am wondering if anyone can share with me an example of multipart/form-data that contains:

  1. Some form parameters
  2. Multiple files

解决方案

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 an user agent like a browser or cURL.

Save the form to an .html file:

<form action="http://localhost:8000" method="post" enctype="multipart/form-data">
  <p><input type="text" name="text" value="text default">
  <p><input type="file" name="file1">
  <p><input type="file" name="file2">
  <p><button type="submit">Submit</button>
</form>

Create files to upload:

echo 'Content of a.txt.' > a.txt
echo '<!DOCTYPE html><title>Content of a.html.</title>' > a.html

Run:

nc -l localhost 8000

Open the HTML on your browser, select the files and click on submit and check the terminal.

nc prints the request received. Firefox sent:

POST / HTTP/1.1
Host: localhost:8000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:29.0) Gecko/20100101 Firefox/29.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Cookie: __atuvc=34%7C7; permanent=0; _gitlab_session=226ad8a0be43681acf38c2fab9497240; __profilin=p%3Dt; request_method=GET
Connection: keep-alive
Content-Type: multipart/form-data; boundary=---------------------------9051914041544843365972754266
Content-Length: 554

-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name="text"

text default
-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name="file1"; filename="a.txt"
Content-Type: text/plain

Content of a.txt.

-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name="file2"; filename="a.html"
Content-Type: text/html

<!DOCTYPE html><title>Content of a.html.</title>

-----------------------------9051914041544843365972754266--

Aternativelly, cURL should send the same POST request as your a browser form:

nc -l localhost 8000
curl -F "text=default" -F "file1=@a.html" -F "file1=@a.txt" localhost:8000

You can do multiple tests with:

while true; do printf '' | nc -l localhost 8000; done

这篇关于多部分/表单数据的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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