How does HTTP file upload work?
来源:互联网 发布:剑三捏脸正太数据 编辑:程序博客网 时间:2024/06/05 20:57
转自:http://stackoverflow.com/questions/8659808/how-does-http-file-upload-work
How does HTTP file upload work?
When I submit a simple form like this with a file attached:
<form enctype="multipart/form-data" action="http://localhost:3000/upload?upload_progress_id=12344" method="POST"><input type="hidden" name="MAX_FILE_SIZE" value="100000" />Choose a file to upload: <input name="uploadedfile" type="file" /><br /><input type="submit" value="Upload File" /></form>
How does it send the file internally? Is the file sent as part of the HTTP body as data? In the headers of this request, I don't see anything related to the name of the file.
I just would like the know the internal workings of the HTTP when sending a file.
4 Answers
Let's take a look at what happens when you select a file and submit your form (I've truncated the headers for brevity):
POST /upload?upload_progress_id=12344 HTTP/1.1Host: localhost:3000Content-Length: 1325Origin: http://localhost:3000Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryePkpFF7tjBAqx29L<other headers>------WebKitFormBoundaryePkpFF7tjBAqx29LContent-Disposition: form-data; name="MAX_FILE_SIZE"100000------WebKitFormBoundaryePkpFF7tjBAqx29LContent-Disposition: form-data; name="uploadedfile"; filename="hello.o"Content-Type: application/x-object<file data>------WebKitFormBoundaryePkpFF7tjBAqx29L--
Instead of URL encoding the form parameters, the form parameters (including the file data) are sent as sections in a multipart document in the body of the request.
In the example above, you can see the input MAX_FILE_SIZE
with the value set in the form, as well as a section containing the file data. The file name is part of the Content-Disposition
header.
The full details are here.
How does it send the file internally?
The format is called multipart/form-data
, as asked at: What does enctype='multipart/form-data' mean?
Once you see some examples of it, it will be really easy to understand how it works.
You can produce examples using 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.txtecho '<!DOCTYPE html><title>Content of a.html.</title>' > a.html
Run:
while true; do printf '' | nc -l localhost 8000; done
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.1Host: localhost:8000User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:29.0) Gecko/20100101 Firefox/29.0Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8Accept-Language: en-US,en;q=0.5Accept-Encoding: gzip, deflateCookie: __atuvc=34%7C7; permanent=0; _gitlab_session=226ad8a0be43681acf38c2fab9497240; __profilin=p%3Dt; request_method=GETConnection: keep-aliveContent-Type: multipart/form-data; boundary=---------------------------9051914041544843365972754266Content-Length: 554-----------------------------9051914041544843365972754266Content-Disposition: form-data; name="text"text default-----------------------------9051914041544843365972754266Content-Disposition: form-data; name="file1"; filename="a.txt"Content-Type: text/plainContent of a.txt.-----------------------------9051914041544843365972754266Content-Disposition: form-data; name="file2"; filename="a.html"Content-Type: text/html<!DOCTYPE html><title>Content of a.html.</title>-----------------------------9051914041544843365972754266--
Therefore it is clear that:
Content-Type: multipart/form-data; boundary=---------------------------9051914041544843365972754266
sets the content type tomultipart/form-data
and says that the fields are separated by the givenboundary
string.every field gets some sub headers before its data:
Content-Disposition: form-data;
, the fieldname
, thefilename
, followed by the data.The server reads the data until the next boundary string. The browser must choose a boundary that will not appear in any of the fields, so this is why the boundary may vary between requests.
An HTTP message may have a body of data sent after the header lines. In a response, this is where the requested resource is returned to the client (the most common use of the message body), or perhaps explanatory text if there's an error. In a request, this is where user-entered data or uploaded files are sent to the server.
http://www.tutorialspoint.com/http/http_messages.htm
- How does HTTP file upload work?
- How Does The Linux File System Work?
- How does netstat work
- How does XVCL work?
- How does JNA work?
- How does cas work
- how does wifi work?
- How does google work
- How does browsersync work?
- How does maven work?
- How does garbage collector work?
- How does a computer work?
- How does an app work?
- how does startup code work
- How ARC does its work?
- How Does Cloudera Manager Work
- HOW DOES A DATABASE WORK
- How Linux file permissions work
- 寻找第k大数
- bzoj 2073: [POI2004]PRZ
- HDOJ 1014 Uniform Generator
- LeetCode-Sort Colors
- c语言命名
- How does HTTP file upload work?
- android-async-http
- Android 自定义View步骤
- Codeforces Round #288 (Div. 2)
- Linux网络设置(参考国嵌视频)
- 作用域
- EF细节处提高性能
- ruby遍历map
- Integer与int的种种比较你知道多少?