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

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

问题描述

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

Hanselman对此 给出了一个很好的解释:


HTTP和文件上传如何通过HTTP工作



对我来说,理解为什么。如果你说只是因为或什么,你只是补充说,而且有效,那么我认为这是可悲的。由于某些原因,很多人都理解FORM POST,一般来说表单数据是如何传递给服务器的,当传输一个文件的时候,很多人都认为这是神奇的。为什么我们必须在包含文件上传的表单上添加enctype =multipart / form = data?因为表单现在会被分成多个部分。



如果您有这样的表单:



 < form action =/ home / uploadfilesmethod =postenctype =multipart / form-data> 
< label for =file>文件名:< / label>
< input type =filename =fileid =file/>
< input type =submitname =submitvalue =Submit/>
< / form>




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




  POST / home / uploadfiles HTTP / 1.1 
Content-Type:multipart / form -数据; boundary = --------------------------- 7d81b516112482
Accept-Encoding:gzip,deflate
User-Agent:Mozilla /4.0(兼容; MSIE 7.0; Windows NT 6.0; WOW64)
内容长度:324

------------------- ---------- 7d81b516112482
Content-Disposition:form-data; NAME = 文件; filename =\\SERVER\Users\Scott\test.txt
Content-Type:text / plain

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

提交
----------------------------- 7d81b516112482--




注意关于这个POST的一些事情。首先,注意内容类型和边界=以及后面如何使用边界,就是多个部分之间的边界。看看第一部分是如何显示我上传了一个text / plain类型的文件。你可以从这里插入,如果它们都是同时发布的话,你将会看到多个文件出现。



当然,看看如果不同,



 <$ c $包含了enctype =multipart / form = data c> POST / home / uploadfiles HTTP / 1.1 
内容类型:application / x-www-form-urlencoded
UA-CPU:x86
Accept-Encoding:gzip,deflate
用户代理:Mozilla / 4.0(兼容; MSIE 7.0; Windows NT 6.0; WOW64)
内容长度:13

提交=提交




查看内容类型是如何不同的?这是一个常规的,典型的POST形式。也许非典型的,它只包括一个提交按钮!
....



顺便说一句,如果你用多个附加文件查看你的电子邮件,它会看起来非常类似于第一个作为多部分MIME编码的HTTP消息随处可见,这是大多数好主意的常见现象。



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="\\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.

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

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