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

前端 未结 11 2415
盖世英雄少女心
盖世英雄少女心 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: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.

    
    

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

    
    

    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.

     
    

    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:

    
    

提交回复
热议问题