HTTP 文件上传是如何工作的?

编程入门 行业动态 更新时间:2024-10-23 21:27:06
本文介绍了HTTP 文件上传是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我提交这样一个带有文件的简单表单时:

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 文件上传是如何工作的?

本文发布于:2023-11-28 15:37:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642989.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件上传   工作   HTTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!