当我提交这样一个带有文件的简单表单时:
When I submit a simple form like this with a file attached:
<form enctype="multipart/form-data" action="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>它如何在内部发送文件?文件是否作为数据作为 HTTP 正文的一部分发送?在此请求的标头中,我没有看到与文件名称相关的任何内容.
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.
我只想知道发送文件时 HTTP 的内部工作原理.
I just would like the know the internal workings of the HTTP when sending a file.
推荐答案让我们来看看当你选择一个文件并提交你的表单时会发生什么(为了简洁起见,我已经截断了标题):
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.1 Host: localhost:3000 Content-Length: 1325 Origin: localhost:3000 ... other headers ... Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryePkpFF7tjBAqx29L ------WebKitFormBoundaryePkpFF7tjBAqx29L Content-Disposition: form-data; name="MAX_FILE_SIZE" 100000 ------WebKitFormBoundaryePkpFF7tjBAqx29L Content-Disposition: form-data; name="uploadedfile"; filename="hello.o" Content-Type: application/x-object ... contents of file goes here ... ------WebKitFormBoundaryePkpFF7tjBAqx29L--注意:每个边界字符串必须以额外的 -- 为前缀,就像在最后一个边界字符串的末尾一样.上面的例子已经包含了这一点,但很容易错过.请参阅下面@Andreas 的评论.
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.
不是对表单参数进行 URL 编码,而是将表单参数(包括文件数据)作为请求正文中的多部分文档中的部分发送.
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.
在上面的示例中,您可以看到输入 MAX_FILE_SIZE 和表单中设置的值,以及包含文件数据的部分.文件名是 Content-Disposition 标头的一部分.
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.
完整的细节在这里.
更多推荐
HTTP 文件上传是如何工作的?
发布评论