Html enctype='multipart/form-data' 是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4526273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 05:50:33  来源:igfitidea点击:

What does enctype='multipart/form-data' mean?

htmlhttp-headersmultipartform-data

提问by EBAG

What does enctype='multipart/form-data'mean in an HTML form and when should we use it?

enctype='multipart/form-data'在 HTML 表单中是什么意思,我们什么时候应该使用它?

回答by Quentin

When you make a POST request, you have to encode the data that forms the body of the request in some way.

当您发出 POST 请求时,您必须以某种方式对构成请求正文的数据进行编码。

HTML forms provide three methods of encoding.

HTML 表单提供了三种编码方法。

  • application/x-www-form-urlencoded(the default)
  • multipart/form-data
  • text/plain
  • application/x-www-form-urlencoded(默认)
  • multipart/form-data
  • text/plain

Work was being done on adding application/json, but that has been abandoned.

添加 的工作正在进行中application/json,但已被放弃。

(Other encodings are possible with HTTP requests generated using other means than an HTML form submission. JSON is a common format for use with web services and some still use SOAP.)

(使用 HTML 表单提交以外的其他方式生成的 HTTP 请求也可以使用其他编码。JSON 是用于 Web 服务的常用格式,有些仍在使用 SOAP。)

The specifics of the formats don't matter to most developers. The important points are:

格式的细节对大多数开发人员来说并不重要。要点是:

  • Never use text/plain.
  • 永远不要使用text/plain.

When you are writing client-side code:

在编写客户端代码时:

  • use multipart/form-datawhen your form includes any <input type="file">elements
  • otherwise you can use multipart/form-dataor application/x-www-form-urlencodedbut application/x-www-form-urlencodedwill be more efficient
  • 使用multipart/form-data时,你的形式包括任何<input type="file">元素
  • 否则你可以使用multipart/form-dataorapplication/x-www-form-urlencodedapplication/x-www-form-urlencoded会更有效率

When you are writing server-side code:

在编写服务器端代码时:

  • Use a prewritten form handling library
  • 使用预先编写的表单处理库

Most (such as Perl's CGI->paramor the one exposed by PHP's $_POSTsuperglobal) will take care of the differences for you. Don't bother trying to parse the raw input received by the server.

大多数(例如 Perl 的CGI->param或 PHP 的$_POSTsuperglobal公开的)会为您处理差异。不要费心去解析服务器收到的原始输入。

Sometimes you will find a library that can't handle both formats. Node.js's most popular library for handling form data is body-parserwhich cannot handle multipart requests (but has documentation which recommends some alternatives which can).

有时您会发现一个库不能同时处理这两种格式。Node.js 最流行的处理表单数据的库是body-parser,它不能处理多部分请求(但有文档推荐了一些可以的替代方案)。



If you are writing (or debugging) a library for parsing or generating the raw data, then you need to start worrying about the format. You might also want to know about it for interest's sake.

如果您正在编写(或调试)用于解析或生成原始数据的库,那么您需要开始担心格式。出于兴趣,您可能还想了解它。

application/x-www-form-urlencodedis more or less the same as a query string on the end of the URL.

application/x-www-form-urlencoded或多或少与 URL 末尾的查询字符串相同。

multipart/form-datais significantly more complicated but it allows entire files to be included in the data. An example of the result can be found in the HTML 4 specification.

multipart/form-data复杂得多,但它允许将整个文件包含在数据中。结果的示例可以在HTML 4 规范中找到

text/plainis introduced by HTML 5 and is useful only for debugging — from the spec: They are not reliably interpretable by computer— and I'd argue that the others combined with tools (like the Network Panelin the developer tools of most browsers) are better for that).

text/plain由 HTML 5 引入,仅用于调试——来自规范它们不能被计算机可靠地解释——我认为其他与工具结合的工具(如大多数浏览器开发工具中的网络面板)更好为了那个原因)。

回答by Matt Asbury

enctype='multipart/form-datais an encoding type that allows files to be sent through a POST. Quite simply, without this encoding the files cannot be sent through POST.

enctype='multipart/form-data是一种编码类型,允许通过POST发送文件。很简单,如果没有这种编码,文件就不能通过POST发送。

If you want to allow a user to upload a file via a form, you must use this enctype.

如果要允许用户通过表单上传文件,则必须使用此enctype

回答by Andry

When submitting a form, you tell your browser to send, via the HTTP protocol, a message on the network, properly enveloped in a TCP/IP protocol message structure. An HTML page has a way to send data to the server: by using <form>s.

提交表单时,您告诉浏览器通过 HTTP 协议在网络上发送一条消息,并正确封装在 TCP/IP 协议消息结构中。HTML 页面有一种向服务器发送数据的方法:通过使用<form>s。

When a form is submitted, an HTTP Request is created and sent to the server, the message will contain the field names in the form and the values filled in by the user. This transmission can happen with POSTor GETHTTP methods.

提交表单时,会创建一个 HTTP 请求并将其发送到服务器,消息将包含表单中的字段名称和用户填写的值。这种传输可以使用POSTGETHTTP 方法进行

  • POSTtells your browser to build an HTTP message and put all content in the body of the message (a very useful way of doing things, more safe and also flexible).
  • GETwill submit the form data in the querystring. It has some constraints about data representation and length.
  • POST告诉您的浏览器构建一个 HTTP 消息并将所有内容放在消息正文中(一种非常有用的做事方式,更安全也更灵活)。
  • GET将在查询字符串中提交表单数据。它对数据表示和长度有一些限制。

Stating how to send your form to the server

说明如何将表单发送到服务器

Attribute enctypehas sense only when using POSTmethod. When specified, it instructs the browser to send the form by encoding its content in a specific way. From MDN - Form enctype:

属性enctype只有在使用POST方法时才有意义。指定后,它会指示浏览器通过以特定方式对其内容进行编码来发送表单。来自MDN - 表单 enctype

When the value of the method attribute is post, enctype is the MIME type of content that is used to submit the form to the server.

当method 属性的值为post 时,enctype 是用于向服务器提交表单的内容的MIME 类型。

  • application/x-www-form-urlencoded: This is the default. When the form is sent, all names and values are collected and URL Encodingis performed on the final string.
  • multipart/form-data: Characters are NOT encoded. This is important when the form has a file upload control. You want to send the file binary and this ensures that bitstream is not altered.
  • text/plain: Spaces get converted, but no more encoding is performed.
  • application/x-www-form-urlencoded: 这是默认的。发送表单时,会收集所有名称和值,并对最终字符串执行URL 编码
  • multipart/form-data: 字符未编码。当表单具有文件上传控件时,这一点很重要。您想发送文件二进制文件,这可确保比特流不会改变。
  • text/plain: 空格被转换,但不再执行编码。

Security

安全

When submitting forms, some security concerns can arise as stated in RFC 7578 Section 7: Multipart form data - Security considerations:

提交表单时,可能会出现一些安全问题,如RFC 7578 第 7 节:多部分表单数据 - 安全考虑

All form-processing software should treat user supplied form-data
with sensitivity, as it often contains confidential or personally
identifying information. There is widespread use of form "auto-fill" features in web browsers; these might be used to trick users to
unknowingly send confidential information when completing otherwise
innocuous tasks. multipart/form-data does not supply any features
for checking integrity, ensuring confidentiality, avoiding user
confusion, or other security features; those concerns must be
addressed by the form-filling and form-data-interpreting applications.

Applications that receive forms and process them must be careful not to supply data back to the requesting form-processing site that was not intended to be sent.

It is important when interpreting the filename of the Content-
Disposition header field to not inadvertently overwrite files in the
recipient's file space.

所有表单处理软件都应
谨慎对待用户提供的表单数据,因为它通常包含机密或个人
身份信息。Web 浏览器中广泛使用表单“自动填充”功能;这些可能会被用来欺骗用户
在完成其他
无害的任务时在不知不觉中发送机密信息。multipart/form-data 不提供任何
用于检查完整性、确保机密性、避免用户
混淆或其他安全功能的功能;这些问题必须
通过表格填写和表格数据解释应用程序来解决。

接收表单并处理它们的应用程序必须小心不要将数据提供回请求表单处理站点,而这些数据并不打算发送。

在解释 Content-
Disposition 标头字段的文件名时,重要的是不要无意中覆盖
收件人文件空间中的文件。

This concerns you if you are a developer and your server will process forms submitted by users which might end up containing sensitive information.

如果您是开发人员并且您的服务器将处理用户提交的表单,这些表单最终可能包含敏感信息,这与您有关。

回答by GP Singh

enctype='multipart/form-data'means that no characters will be encoded. that is why this type is used while uploading files to server.
So multipart/form-datais used when a form requires binary data, like the contents of a file, to be uploaded

enctype='multipart/form-data'意味着不会编码任何字符。这就是为什么在将文件上传到服务器时使用这种类型的原因。
Somultipart/form-data用于表单需要上传二进制数据(如文件内容)时

回答by sandy

Set the method attribute to POST because file content can't be put inside a URL parameter using a form.

将 method 属性设置为 POST,因为不能使用表单将文件内容放入 URL 参数中。

Set the value of enctype to multipart/form-data because the data will be split into multiple parts, one for each file plus one for the text of the form body that may be sent with them.

将 enctype 的值设置为 multipart/form-data 因为数据将被分成多个部分,一个用于每个文件,另一种用于可能与它们一起发送的表单正文的文本。

回答by Premraj

  • enctype(ENCode TYPE) attribute specifies how the form-data should be encoded when submitting it to the server.
  • multipart/form-datais one of the value of enctype attribute, which is used in form element that have a file upload. multi-partmeans form data divides into multiple partsand send to server.
  • enctype( ENCode TYPE) 属性指定表单数据在提交到服务器时应如何编码。
  • multipart/form-data是 enctype 属性的值之一,用于有文件上传的表单元素。多部分是指表单数据分成多个部分并发送到服务器。

回答by Eric

Usually this is when you have a POST form which needs to take a file upload as data... this will tell the server how it will encode the data transferred, in such case it won't get encoded because it will just transfer and upload the files to the server, Like for example when uploading an image or a pdf

通常这是当您有一个 POST 表单需要将文件上传作为数据时......这将告诉服务器它将如何对传输的数据进行编码,在这种情况下它不会被编码,因为它只会传输和上传文件到服务器,例如上传图像或 pdf 时

回答by Rishad

The enctype attribute specifies how the form-data should be encoded when submitting it to the server.

The enctype attribute can be used only if method="post".

No characters are encoded. This value is required when you are using forms that have a file upload control

enctype 属性指定表单数据在提交到服务器时应如何编码。

enctype 属性只能在 method="post" 时使用。

没有字符被编码。当您使用具有文件上传控件的表单时,此值是必需的

From W3Schools

来自W3Schools