Limit file format when using <input type=“file”>?

前端 未结 11 2232
盖世英雄少女心
盖世英雄少女心 2020-11-22 04:15

I\'d like to restrict the type of file that can be chosen from the native OS file chooser when the user clicks the Browse button in the

相关标签:
11条回答
  • 2020-11-22 04:45

    Technically you can specify the accept attribute (alternative in html5) on the input element, but it's not properly supported.

    0 讨论(0)
  • 2020-11-22 04:48

    Use input tag with accept attribute

    <input type="file" name="my-image" id="image" accept="image/gif, image/jpeg, image/png" />
    

    Click here for the latest browser compatibility table

    Live demo here

    To select only image files, you can use this accept="image/*"

    <input type="file" name="my-image" id="image" accept="image/*" />
    

    Live demo here

    Only gif, jpg and png will be shown, screen grab from Chrome version 44

    0 讨论(0)
  • 2020-11-22 04:49

    I may suggest following:

    • If you have to make user select any of image files by default, the use accept="image/*"

      <input type="file" accept="image/*" />

    • if you want to restrict to specific image types then use accept="image/bmp, image/jpeg, image/png"

      <input type="file" accept="image/bmp, image/jpeg, image/png" />

    • if you want to restrict to specific types then use accept=".bmp, .doc, .pdf"

      <input type="file" accept=".bmp, .doc, .pdf" />

    • You cannot restrict user to change file filer to all files, so always validate file type in script and server

    0 讨论(0)
  • 2020-11-22 04:54

    I know this is a bit late.

    function Validatebodypanelbumper(theForm)
    {
       var regexp;
       var extension =     theForm.FileUpload.value.substr(theForm.FileUpload1.value.lastIndexOf('.'));
       if ((extension.toLowerCase() != ".gif") &&
           (extension.toLowerCase() != ".jpg") &&
           (extension != ""))
       {
          alert("The \"FileUpload\" field contains an unapproved filename.");
          theForm.FileUpload1.focus();
          return false;
       }
       return true;
    }
    
    0 讨论(0)
  • 2020-11-22 04:54

    You can use "accept" attribute as a filter in the file select box. Using "accept" help you filter input files base on their "suffix" or their "meme type"

    1.Filter based on suffix: Here "accept" attribute just allow to select files with .jpeg extension.

    <input type="file" accept=".jpeg" />
    

    2.Filter based on "file type" Here "accept" attribute just allow to select file with "image/jpeg" type.

    <input type="file" accept="image/jpeg" />
    

    Important: We can change or delete the extension of a file, without changing the meme type. For example it is possible to have a file without extension, but the type of this file can be "image/jpeg". So this file can not pass the accept=".jpeg" filter. but it can pass accept="image/jpeg".

    3.We can use * to select all kind of a file type. For example below code allow to select all kind of images. for example "image/png" or "image/jpeg" or ... . All of them are allowed.

    <input type="file" accept="image/*" /> 
    

    4.We can use cama ( , ) as an "or operator" in select attribute. For example to allow all kind of images or pdf files we can use this code:

    <input type="file" accept="image/* , application/pdf" />
    
    0 讨论(0)
  • 2020-11-22 04:55

    Strictly speaking, the answer is no. A developer cannot prevent a user from uploading files of any type or extension.

    But still, the accept attribute of <input type = "file"> can help to provide a filter in the file select dialog box of the OS. For example,

    <!-- (IE 10+, Edge (EdgeHTML), Edge (Chromium), Chrome, Firefox 42+) -->
    <input type="file" accept=".xls,.xlsx" />

    should provide a way to filter out files other than .xls or .xlsx. Although the MDN page for input element always said that it supports this, to my surprise, this didn't work for me in Firefox until version 42. This works in IE 10+, Edge, and Chrome.

    So, for supporting Firefox older than 42 along with IE 10+, Edge, Chrome, and Opera, I guess it's better to use comma-separated list of MIME-types:

    <!-- (IE 10+, Edge (EdgeHTML), Edge (Chromium), Chrome, Firefox) -->
    <input type="file"
     accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel" /> 

    [Edge (EdgeHTML) behavior: The file type filter dropdown shows the file types mentioned here, but is not the default in the dropdown. The default filter is All files (*).]

    You can also use asterisks in MIME-types. For example:

    <input type="file" accept="image/*" /> <!-- all image types --> 
    <input type="file" accept="audio/*" /> <!-- all audio types --> 
    <input type="file" accept="video/*" /> <!-- all video types --> 

    W3C recommends authors to specify both MIME-types and corresponding extensions in the accept attribute. So, the best approach is:

    <!-- Right approach: Use both file extensions and corresponding MIME-types. -->
    <!-- (IE 10+, Edge (EdgeHTML), Edge (Chromium), Chrome, Firefox) -->
    <input type="file"
     accept=".xls,.xlsx, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel" /> 

    JSFiddle of the same: here.

    Reference: List of MIME-types

    IMPORTANT: Using the accept attribute only provides a way of filtering in the files of types that are of interest. Browsers still allow users to choose files of any type. Additional (client-side) checks should be done (using JavaScript, one way would be this), and definitely file types MUST be verified on the server, using a combination of MIME-type using both the file extension and its binary signature (ASP.NET, PHP, Ruby, Java). You might also want to refer to these tables for file types and their magic numbers, to perform a more robust server-side verification.

    Here are three good reads on file-uploads and security.

    EDIT: Maybe file type verification using its binary signature can also be done on client side using JavaScript (rather than just by looking at the extension) using HTML5 File API, but still, the file must be verified on the server, because a malicious user will still be able to upload files by making a custom HTTP request.

    0 讨论(0)
提交回复
热议问题