2010-11-21 21 views
58

tôi tự hỏi nếu có ai có thể chia sẻ với tôi một ví dụ về multipart/form-data có chứa:Ví dụ về multipart/form-data

  1. Một số tham số hình thức
  2. Nhiều file
+1

Xem http:. //www.htmlcodetutorial. com/forms/form_enctype.html –

+0

bản sao có thể có của [Yêu cầu HTTP Multipart với nhiều tệp như thế nào?] (http://stackoverflow.com/questions/913626/what-should-a-multipart-http-request- with-multiple-files-look-like) –

+1

Truy cập tại đây: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4 Trong '17.13.4 Loại nội dung biểu mẫu' bạn sẽ tìm thấy những gì bạn tìm kiếm –

Trả lời

77

EDIT: Tôi đang duy trì một câu trả lời tương tự, nhưng sâu hơn tại: https://stackoverflow.com/a/28380690/895245

Để xem chính xác những gì đang xảy ra, bạn se nc -l hoặc máy chủ ECHO và tác nhân người dùng như trình duyệt hoặc cURL.

Lưu dưới dạng một .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> 

Tạo file upload:

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

Run:

nc -l localhost 8000 

Mở HTML trên trình duyệt của bạn, chọn các tập tin và nhấp vào gửi và kiểm tra thiết bị đầu cuối.

nc in yêu cầu đã nhận. Firefox gửi:

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 nên gửi yêu cầu POST giống như một hình thức trình duyệt của bạn:

nc -l localhost 8000 
curl -F "text=default" -F "[email protected]" -F "[email protected]" localhost:8000 

Bạn có thể làm nhiều thử nghiệm với:

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

Nội dung không rõ ràng và không rõ ràng: 'ranh giới = --------------------------- 9051914041544843365972754266' là * hai dấu gạch ngang ngắn hơn * sau đó là ranh giới thực tế trong dữ liệu. Điều này thực sự, ** thực sự ** khó nhìn thấy với tất cả các dấu gạch ngang nối với nhau. –

3

Nhiều nhờ @ Ciro Santilli trả lời! Tôi thấy rằng sự lựa chọn của mình cho ranh giới là khá "không hài lòng" bởi vì tất cả các dấu gạch ngang thoose: trên thực tế, như @Fake Name nhận xét, khi bạn đang sử dụng ranh giới của bạn bên trong yêu cầu nó đi kèm với hai dấu gạch ngang hơn ở phía trước:

Ví dụ:

POST/HTTP/1.1 
HOST: host.example.com 
Cookie: some_cookies... 
Connection: Keep-Alive 
Content-Type: multipart/form-data; boundary=12345 

--12345 
Content-Disposition: form-data; name="sometext" 

some text that you wrote in your html form ... 
--12345 
Content-Disposition: form-data; name="name_of_post_request" filename="filename.xyz" 

content of filename.xyz that you upload in your form with input[type=file] 
--12345 
Content-Disposition: form-data; name="image" filename="picture_of_sunset.jpg" 

content of picture_of_sunset.jpg ... 
--12345-- 

tôi thấy on this w3.org page rằng có thể incapsulate multipart/mixed tiêu đề trong một multipart/form-data, bạn chỉ cần chọn một chuỗi ranh giới bên trong multipart/mixed và sử dụng một trong đó để incapsulate dữ liệu. Cuối cùng, bạn phải "đóng" tất cả các ranh giới sử dụng theo thứ tự Filo để đóng yêu cầu POST (như:

POST/HTTP/1.1 
... 
Content-Type: multipart/form-data; boundary=12345 

--12345 
Content-Disposition: form-data; name="sometext" 

some text sent via post... 
--12345 
Content-Disposition: form-data; name="files" 
Content-Type: multipart/mixed; boundary=abcde 

--abcde 
Content-Disposition: file; file="picture.jpg" 

content of jpg... 
--abcde 
Content-Disposition: file; file="test.py" 

content of test.py file .... 
--abcde-- 
--12345-- 

Hãy nhìn vào liên kết ở trên

+0

Tại sao bạn không phân tách tất cả các thuộc tính trong 'Nội dung-Bố trí' bằng'; '? – kelin