File input 'accept' attribute - is it useful?

前端 未结 8 1635
独厮守ぢ
独厮守ぢ 2020-11-22 02:30

Implementing a file upload under html is fairly simple, but I just noticed that there is an \'accept\' attribute that can be added to the

8条回答
  •  攒了一身酷
    2020-11-22 02:57

    In 2015 the only way I found to make it work for both Chrome and Firefox is to put all possible extensions you want to support, including variants (including the dot in front !):

    accept=".jpeg, .jpg, .jpe, .jfif, .jif"
    

    Problem with Firefox: Using the image/jpeg mime type Firefox will only show .jpg files, very strange as if the common .jpeg was not ok...

    Whatever you do, be sure to try with files having many different extensions. Maybe it even depends on the OS ... I suppose accept is case insensitive, but maybe not in every browser.

    Here is the MDN docs about accept:

    accept If the value of the type attribute is file, then this attribute will indicate the types of files that the server accepts, otherwise it will be ignored. The value must be a comma-separated list of unique content type specifiers:

        A file extension starting with the STOP character (U+002E). (e.g. .jpg, .png, .doc).
        A valid MIME type with no extensions.
        audio/* representing sound files. HTML5
        video/* representing video files. HTML5
        image/* representing image files. HTML5
    

提交回复
热议问题