使用`enctype =" multipart / form-data"总是或从不? [英] Use `enctype="multipart/form-data"` always or never?

查看:176
本文介绍了使用`enctype =" multipart / form-data"总是或从不?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我知道你以前需要设置 enctype =multipart / form-数据< form> 标记中, p>我想在我的通用模板中避免这种情况。



我该怎么办?我看到这些解决方案︰


  • 使用 enctype =multipart / form-data总是如此。
  • 使用 enctype =multipart / form-data从不。


  • 背景:我很幸运,我不需要支持旧的浏览器。我不需要支持IE9或更旧的版本。

    解决方案

    当在窗体中上传文件时,应该将编码指定为multipart / form-data。如果你想保持你的表单是通用的,你可以在表单中省略这个属性,并直接使用 formenctype属性的输入或按​​钮元素(只适用于支持HTML5的浏览器)

    这:

     < form action =upload.phpmethod =postenctype =multipart / form-data > 
    选择要上传的图片:
    < input type =filename =fileToUploadid =fileToUpload>
    < input type =submitvalue =Upload Imagename =submit>
    < / form>

    至此:

     < form action =upload.phpmethod =post> 
    选择要上传的图片:
    < input type =filename =fileToUploadid =fileToUploadformenctype =multipart / form-data>
    < input type =submitvalue =Upload Imagename =submit>
    < / form>

    另外,您可以在建议的地方检查这个问题,以避免总是使用 enctype =multipart / form-data为什么不总是使用enctype =" multipart / form-data"


    I want to write a generic html template.

    I know that in the past you needed to set enctype="multipart/form-data" in the <form> tag, if you wanted to upload files.

    I would like to avoid this condition in my generic template.

    What should I do? I see these solutions:

    • use enctype="multipart/form-data" always.
    • use enctype="multipart/form-data" never.

    Background: I am lucky, I don't need to support old browsers. I don't need to support IE9 or older versions.

    解决方案

    When uploading a file in your form you should specify the encoding as "multipart/form-data". If you want to keep your form generic, you can omit this attribute in the form and override it directly using formenctype attribute of input or button elements (only possible in browsers with HTML5 support)

    You can chage this:

    <form action="upload.php" method="post" enctype="multipart/form-data">
      Select image to upload:
      <input type="file" name="fileToUpload" id="fileToUpload">
      <input type="submit" value="Upload Image" name="submit">
    </form>
    

    To this:

    <form action="upload.php" method="post">
      Select image to upload:
      <input type="file" name="fileToUpload" id="fileToUpload" formenctype="multipart/form-data">
      <input type="submit" value="Upload Image" name="submit">
    </form>
    

    Also, you can check this question where is recommended to avoid using always enctype="multipart/form-data": Why not always use enctype="multipart/form-data"?

    这篇关于使用`enctype =&quot; multipart / form-data&quot;总是或从不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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