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.
Best Answer
Let's take a look at what happens when you select a file and submit your form (I've truncated the headers for brevity):
NOTE: each boundary string must be prefixed with an extra
--
, just like in the end of the last boundary string. The example above already includes this, but it can be easy to miss. See comment by @Andreas below.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 theContent-Disposition
header.The full details are here.