enctype='multipart/form-data' 是什么意思? [英] What does enctype='multipart/form-data' mean?

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

问题描述

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

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

推荐答案

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

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

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

HTML forms provide three methods of encoding.

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

正在添加application/json,但已被放弃.

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

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

(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.)

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

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

  • 永远不要使用text/plain.

当您编写客户端代码时:

When you are writing client-side code:

  • 当您的表单包含任何 元素时,请使用 multipart/form-data
  • 否则你可以使用 multipart/form-dataapplication/x-www-form-urlencodedapplication/x-www-form-urlencoded 会更有效率
  • use multipart/form-data when your form includes any <input type="file"> elements
  • otherwise you can use multipart/form-data or application/x-www-form-urlencoded but application/x-www-form-urlencoded will be more efficient

当您编写服务器端代码时:

When you are writing server-side code:

  • 使用预先编写的表单处理库

大多数(例如 Perl 的 CGI->param 或 PHP 的 $_POST 超全局变量)会为您处理差异.不要费心去解析服务器收到的原始输入.

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

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

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

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

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-urlencoded 或多或少与 URL 末尾的查询字符串相同.

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

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

multipart/form-data is 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.

text/plain 由 HTML 5 引入,仅用于调试 — 来自 规范:它们不能被计算机可靠地解释——我认为其他的结合了工具(比如 网络面板 对此更好).

text/plain is 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 Panel in the developer tools of most browsers) are better for that).

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

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