html - 为什么上传文件时需要form enctype=multipart/form-data?

标签 html http file-upload specifications rfc

为什么是<form enctype=multipart/form-data>将文件上传到网络服务器时需要吗?

最佳答案

它与浏览器如何打包二进制和表单数据以通过 HTTP 传输有关。默认情况下只发送表单数据,但如果表单需要支持上传文件,则还必须附加二进制数据并将其与表单数据分开。

Scott Hanselman 对此给出了很好的解释 here :

HTTP and How File Upload works via HTTP

It's always better, for me, to understand WHY and HOW something is happening. If you say "just because" or "whatever, you just add that, and it works" then I think that's sad. For some reason while many folks understand FORM POSTs and generally how form data is passed up to the server, when a file is transferred many just conclude it's magic. Why do we have to add enctype="multipart/form=data" on our forms that include file uploads? Because the form will now be POSTed in multiple parts.

If you have a form like this:

<form action="/home/uploadfiles" method="post" enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file" />
    <input type="submit" name="submit" value="Submit" />
</form>

The resulting Form POST will look like this (slightly simplified):

POST /home/uploadfiles HTTP/1.1
Content-Type: multipart/form-data; boundary=---------------------------7d81b516112482 
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64)
Content-Length: 324

-----------------------------7d81b516112482 
Content-Disposition: form-data; name="file"; filename="\\SERVER\Users\Scott\test.txt"
Content-Type: text/plain

foo
-----------------------------7d81b516112482
Content-Disposition: form-data; name="submit"

Submit
-----------------------------7d81b516112482--

Notice a few things about this POST. First, notice the content-type and boundary="" and how the boundary is used later, as exactly that, a boundary between the multiple parts. See how the first part shows that I uploaded a single file, of type text/plain. You can interpolate from this how you'd expect multiple files to show up if they were all POSTed at once.

And of course, look at how different this would look if it were just a basic form POST without the enctype="multipart/form=data" included:

POST /home/uploadfiles HTTP/1.1 
Content-Type: application/x-www-form-urlencoded
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64)
Content-Length: 13

submit=Submit

See how the content type is different? This is a regular, typical form POST. Perhaps atypical in that it includes only a Submit button! ....

As an aside, if you looked at an email of yours with multiple attached files, it would look VERY similar to the body of the first HTTP message as multipart MIME encoding is found everywhere, as is common with most good ideas.

关于html - 为什么上传文件时需要form enctype=multipart/form-data?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1342506/

相关文章:

可选 24 或 12 小时的 jQuery 实时时钟

javascript - 启用 Chrome 插件后,如何在我网站上的图像中隐藏 Pin It 按钮

Java - 在 Web 浏览器中显示 HttpResponse 的内容

http - 使用 Node.JS 提供 HTTP/1.0 响应(内容长度未知,分块传输编码)

ruby-on-rails - 无法使用回形针上传照片

PHP 没有在正确的地方回显

html - 使整个部分可悬停和可点击

http - 状态错误 : Cannot set the body fields of a Request with content-type "application/json"

django - 将文件上传到django中的私有(private)位置

forms - 使用nodejs和connect-form上传文件