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

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

问题描述

enctype ='multipart / form-data' HTML 表单中的含义是什么,我们应该在什么时候使用当你发出一个POST请求时,你必须以某种方式对形成请求主体的数据进行编码。

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


  • application / x-www-form-urlencoded (默认) li>
  • multipart / form-data

  • text / plain

    正在完成添加 application / json ,但已被放弃。



    这些格式的细节对大多数开发人员无关紧要。重要的是:

    在编写客户端代码时,您只需使用 multipart / form-data 当您的表单包含任何< input type =file> 元素。



    在编写服务器端代码时:使用预先写好的表单处理库(例如Perl的 CGI-> param 或由PHP的 $ _ POST superglobal)公开的那个,它会照顾到你的差异。不要试图解析服务器接收到的原始输入。



    切勿使用 text / plain




    如果您正在编写(或调试)库来解析或生成原始数据,那么你需要开始担心格式。您可能也想知道它的利益。



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

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

    text / plain 由HTML 5引入,仅用于调试 - 规范它们不能被计算机可靠解释 em> - 我认为其他人与工具结合在一起(比如大多数浏览器的开发人员工具中的Net选项卡)会更好。)


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

    解决方案

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

    HTML forms provide three methods of encoding.

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

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

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

    When you are writing client-side code, all you need to know is use multipart/form-data when your form includes any <input type="file"> elements.

    When you are writing server-side code: Use a prewritten form handling library (e.g. Perl's CGI->param or the one exposed by PHP's $_POST superglobal) and it will take care of the differences for you. Don't bother trying to parse the raw input received by the server.

    Never use text/plain.


    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.

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

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

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