Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

html - enctype ='multipart / form-data'是什么意思?(What does enctype='multipart/form-data' mean?)

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

  ask by EBAG translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

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

((使用HTML表单提交以外的其他方式生成的HTTP请求也可以使用其他编码。))

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-data when your form includes any <input type="file"> elements

    (当表单包含任何<input type="file">元素时,请使用multipart/form-data)

  • otherwise you can use multipart/form-data or application/x-www-form-urlencoded but application/x-www-form-urlencoded will be more efficient

    (否则,您可以使用multipart/form-dataapplication/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->param or the one exposed by PHP's $_POST superglobal) will take care of the differences for you.

(大多数(例如Perl的CGI->param或PHP的$_POST超全局变量公开的CGI->param )将为您解决这些差异。)

Don't bother trying to parse the raw input received by the server.

(不要费力尝试解析服务器收到的原始输入。)

Sometimes you will find a library which 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 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-urlencoded is more or less the same as a query string on the end of the URL.

(application/x-www-form-urlencoded与URL末尾的查询字符串大致相同。)

multipart/form-data is significantly more complicated but it allows entire files to be included in the data.

(multipart/form-data要复杂得多,但是它允许整个文件包含在数据中。)

An example of the result can be found in the HTML 4 specification .

(结果的一个示例可以在HTML 4规范中找到 。)

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 Net tab in the developer tools of most browsers) are better for that).

(text/plain由HTML 5引入,仅在调试时有用-从规范上来说它们不能由计算机可靠地解释 -我认为其他结合了工具(例如大多数浏览器的开发人员工具中的Net标签) )对此更好)。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...