码迷,mamicode.com
首页 > 其他好文 > 详细

再次回顾post请求中的enctype

时间:2017-07-04 01:02:36      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:jquer   padding   ons   receive   files   clipboard   highlight   depend   rac   

1,关于multipart/form-data,参考rfc1867

2.关于post的集中编码格式,参考MDN

A brief introduction to the submit methods

An html <form> can be sent in four ways:

  • using the POST method and setting the enctype attribute to application/x-www-form-urlencoded (default);
  • using the POST method and setting the enctype attribute to text/plain;
  • using the POST method and setting the enctype attribute to multipart/form-data;
  • using the GET method (in this case the enctype attribute will be ignored).

Now, consider the submission of a form containing only two fields, named foo and baz. If you are using the POST method the server will receive a string similar to one of the following three examples, depending on the encoding type you are using:

  • Method: POST; Encoding type: application/x-www-form-urlencoded (default):

    Content-Type: application/x-www-form-urlencoded
    
    foo=bar&baz=The+first+line.%0D%0AThe+second+line.%0D%0A
  • Method: POST; Encoding type: text/plain:

    Content-Type: text/plain
    
    foo=bar
    baz=The first line.
    The second line.
  • Method: POST; Encoding type: multipart/form-data:(上传文件会用到)

    Content-Type: multipart/form-data; boundary=---------------------------314911788813839
    
    -----------------------------314911788813839
    Content-Disposition: form-data; name="foo"
    
    bar
    -----------------------------314911788813839
    Content-Disposition: form-data; name="baz"
    
    The first line.
    The second line.
    
    -----------------------------314911788813839--

However, if you are using the GET method, a string like the following will be simply added to the URL:

?foo=bar&baz=The%20first%20line.%0AThe%20second%20line.

关于multipart/form-data有个小插曲,就是在使用Jquery 的Formdata对象进行ajax上传文件时,ajax有个content-type的设置,默认是application/x-www-form-urlencoded,但是,如果上传文件,必须使用multipart/form-data,
而且必须设定分隔符,所以,此项不需要设置(值为false),然后通过HTML表单创建FormData对象即可(var formData = new FormData(someFormElement))
关于


 

multipart/form-data的请求头必须包含一个特殊的头信息:Content-Type,且其值也必须规定为multipart/form-data,同时还需要规定一个内容分割符用于分割请求体中的多个post的内容,如文件内容和文本内容自然需要分割开来,不然接收方就无法正常解析和还原这个文件了。具体的头信息如下:

[html] view plain copy
 
  1. Content-Type: multipart/form-data; boundary=${bound}     

再次回顾post请求中的enctype

标签:jquer   padding   ons   receive   files   clipboard   highlight   depend   rac   

原文地址:http://www.cnblogs.com/ch459742906/p/7113503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!