为什么上传文件时需要form enctype=multipart/form-data? [英] Why is form enctype=multipart/form-data required when uploading a file?

查看:34
本文介绍了为什么上传文件时需要form enctype=multipart/form-data?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在将文件上传到网络服务器时需要

?

解决方案

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

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

<块引用>

HTTP 以及如何通过 HTTP 上传文件

对我来说,了解事情发生的原因和方式总是更好.如果你说只是因为"或无论如何,您只需添加它,它就可以工作";那我觉得这很可悲.出于某种原因,虽然许多人了解 FORM POST 以及通常如何将表单数据传递到服务器,但当文件被传输时,许多人只是认为它很神奇.为什么要加上enctype="multipart/form=data"在我们包含文件上传的表单上?因为表单现在将分多个部分发布.

如果您有这样的表格:

<块引用>

生成的表单 POST 将如下所示(稍微简化):

POST/home/uploadfiles HTTP/1.1内容类型:多部分/表单数据;边界=---------------------------7d81b516112482接受编码:gzip、deflate用户代理:Mozilla/4.0(兼容;MSIE 7.0;Windows NT 6.0;WOW64)内容长度:324-----------------------------7d81b516112482内容配置:表单数据;名称=文件";文件名=\SERVERUsersScott	est.txt";内容类型:文本/纯文本富-----------------------------7d81b516112482内容配置:表单数据;名称=提交"提交-----------------------------7d81b516112482--

<块引用>

请注意有关此 POST 的一些注意事项.首先,注意内容类型和边界=".以及以后如何使用边界,确切地说,是多个部分之间的边界.看看第一部分如何显示我上传了一个文本/纯文本类型的文件.您可以从中推断出,如果一次发布所有文件,您希望如何显示多个文件.

当然,看看如果它只是一个没有 enctype="multipart/form=data" 的基本表单 POST 会有多不同.包括:

POST/home/uploadfiles HTTP/1.1内容类型:应用程序/x-www-form-urlencodedUA-CPU:x86接受编码:gzip、deflate用户代理:Mozilla/4.0(兼容;MSIE 7.0;Windows NT 6.0;WOW64)内容长度:13提交=提交

<块引用>

看看内容类型有什么不同?这是一个常规的、典型的表单 POST.也许不典型,因为它只包含一个提交按钮!....

顺便说一句,如果您查看带有多个附件的电子邮件,它看起来与第一条 HTTP 消息的正文非常相似,因为多部分 MIME 编码随处可见,这在大多数好主意中都很常见.

Why is <form enctype=multipart/form-data> required when uploading a file to a web-server?

解决方案

It has to do with how the browser packages binary and form data for transmission over HTTP. By default only form data is sent, but if the form needs to support uploading a file, then the binary data must also be appended and separated from the form data.

Scott Hanselman gives a good explanation of this 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="\SERVERUsersScott	est.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.

这篇关于为什么上传文件时需要form enctype=multipart/form-data?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆