Set the encoding format for form transmission

Set the encoding format for form transmission 

1591 people read comments (0) Favorite report        
  classification:
 

-------------------------------------------------- ------------------------------------
The meaning of enctype="multipart/form-data" in the form, Is to set the MIME encoding of the form. ( For file upload )

By default, this encoding format is application/x-www-form-urlencoded (cannot be used for file upload);


Only when multipart/form-data is used can the file data be completely transferred.


enctype="multipart/form-data" is to upload binary data; the value of input in the form is entered with 2

The system is passed on.

The input value in the form is passed in binary format,


So the request is not worth it. In other words, if this code is added, the request will be unsuccessful.


When adding the form value to the database , the following is used:

SmartUpload su = new SmartUpload();//Create a new SmartUpload object

su.getRequest().getParameterValues(); take the array value

su.getRequest().getParameter( ); take Single parameter single value

 ------------------------------------------------------------------------------------------------------



 Add the attribute enctype="multipart/form-data" to the <form> of the upload file form ,

 In fact, when you do not write the enctype attribute in the form form, the enctype attribute value is added by default. The default value is enctype="application/x- www-form-urlencoded".

 This attribute manages the MIME encoding of the form. There are three values ​​to choose from:

  ①application/x-www-form-urlencoded (default)

  ②multipart/form-data

  ③text/plain

  where ①application/x-www-form-urlencoded is the default Value, you may have seen this in AJAX: xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); These two things are the same thing, which is to set the form transmission Encoding. If you don't write it in AJAX, you may get an error, but you can leave enctype="application/x-www-form-urlencoded" in the HTML form, because the default HTML form is this type of transmission encoding.


②multipart-form-data是用来指定传输数据的特殊类型的,主要就是我们上传的非文本的内容,比如图片或者mp3等等。


③text/plain是纯文本传输的意思,在发送邮件时要设置这种编码类型,否则会出现接收时编码混乱的问题,网络上经常拿text/plain和 text/html做比较,其实这两个很好区分,前者用来传输纯文本文件,后者则是传递html代码的编码类型,


在发送头文件时才用得上。①和③都不能用 于上传文件,只有multipart/form-data才能完整的传递文件数据。

  上面提到的MIME,它的英文全称是"Multipurpose Internet Mail Extensions" 多功能Internet 邮件扩充服务,它是一种多用途网际邮件扩充协议,在1992年最早应用于电子邮件系统,但后来也应用到浏览器。服务器会将它们发送的多媒体数据的类型告诉 浏览器,而通知手段就是说明该多媒体数据的MIME类型,从而让浏览器知道接收到的信息哪些是MP3文件,哪些是Shockwave文件等等。服务器将 MIME标志符放入传送的数据中来告诉浏览器使用哪种插件读取相关文件。

  简单说,MIME类型就是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自动使用指定应用程序来打开。多用于指定一些客户端自定义的文件名,以及一些媒体文件打开方式。

  浏览器接收到文件后,会进入插件系统进行查找,查找出哪种插件可以识别读取接收到的文件。如果浏览器不清楚调用哪种插件系统,它可能会告诉用户缺少某 插件,或者直接选择某现有插件来试图读取接收到的文件,后者可能会导致系统的崩溃。传输的信息中缺少MIME标识可能导致的情况很难估计,因为某些计算机 系统可能不会出现什么故障,但某些计算机可能就会因此而崩溃。

--------------------------------------------------------------------------------------
表单中enctype="multipart/form-data"的意思,是设置表单的MIME编码。(用于文件上传

默认情况,这个编码格式是application/x-www-form-urlencoded   (不能用于文件上传);


只有使用了multipart/form-data,才能完整的传递文件数据。


enctype="multipart/form-data"是上传二进制数据; form里面的input的值以2进

制的方式传过去。

form里面的input的值以2进制的方式传过去,


所以request就得不到值了。也就是说加了这段代码,用request就会传递不成功


取表单值加入数据库时,用到下面的:

SmartUpload su = new SmartUpload();//新建一个SmartUpload对象

su.getRequest().getParameterValues();取数组值

su.getRequest().getParameter( );取单个参数单个值

 ------------------------------------------------------------------------------------------------------



 上传文件的表单中<form>要加属性enctype="multipart/form-data",

 其实form表单在你不写enctype属性时,也默认为其添加了enctype属性值,默认值是enctype="application/x- www-form-urlencoded".

 这个属性管理的是表单的MIME编码,共有三个值可选:

  ①application/x-www-form-urlencoded (默认值)

  ②multipart/form-data

  ③text/plain

  其中①application/x-www-form-urlencoded是默认值,大家可能在AJAX里见过这 个:xmlHttp.setRequestHeader("Content-Type","application/x-www-form- urlencoded"); 这两个要做的是同一件事情,就是设置表单传输的编码。在AJAX里不写有可能会报错,但是在HTML的form表单里是可以不写 enctype="application/x-www-form-urlencoded"的,因为默认HTML表单就是这种传输编码类型


②multipart-form-data是用来指定传输数据的特殊类型的,主要就是我们上传的非文本的内容,比如图片或者mp3等等。


③Text/plain means plain text transmission. This encoding type should be set when sending mail, otherwise there will be a problem of encoding confusion when receiving. On the Internet, text/plain and text/html are often compared. In fact, these two are very different. It is easy to distinguish, the former is used to transmit plain text files, and the latter is the encoding type for transmitting html code.


Only used when sending header files. Neither ① nor ③ can be used to upload files, only multipart/form-data can transfer file data completely.

  The MIME mentioned above, its full English name is "Multipurpose Internet Mail Extensions" Multipurpose Internet Mail Extensions. It is a multipurpose Internet mail extension protocol . It was first applied to e-mail systems in 1992, but it was also applied later. Browser. The server will tell the browser the type of multimedia data they send, and the notification means is to indicate the MIME type of the multimedia data, so that the browser knows which of the received information are MP3 files, which are Shockwave files, and so on. The server puts the MIME identifier into the transmitted data to tell the browser which plug-in to use to read the relevant file.

  Simply put, the MIME type is the way to set a file with a certain extension to be opened by an application . When the file with the extension is accessed, the browser will automatically use the specified application to open it . It is mostly used to specify some client-defined file names and some media file opening methods.

  After the browser receives the file, it will enter the plug-in system to find out which plug-in can recognize and read the received file. If the browser does not know which plug-in system to call, it may tell the user that a plug-in is missing, or directly select an existing plug-in to try to read the received file, which may cause the system to crash. The situation that may be caused by the lack of MIME identification in the transmitted information is difficult to estimate, because some computer systems may not experience any failure, but some computers may crash due to this.

Guess you like

Origin blog.csdn.net/Qianliwind/article/details/70484011