Input accept=“image/png” is not working in Firefox

后端 未结 5 1989
醉梦人生
醉梦人生 2021-01-17 15:37

jsbin


It\'s expected that the file dialog accepts png files only. But accept=\"image

5条回答
  •  自闭症患者
    2021-01-17 16:12

    As other answers describe, Firefox just does not support type="image/png" yet. Instead, it ignores the attribute and does not apply any file filter. Using type="image/*" would work, but then the filter would allow all image files.

    Perhaps the best practical workaround is to use JavaScript code that checks for the file name extension. It does not really prove anything, but close to 100% certainty, PNG files have names ending with .png and other files don’t. Sample code (replace the rude alert by a function that suit your UI design):

    The downside is that when scripting is turned off, Firefox will accept any file, even though accept="image/*" would at least restrict the files to image files. However, JavaScript being disabled is probably rare enough, as compared with the trouble of browser sniffing needed to server different type attributes to different browsers.

    You should naturally check the file types on the server before doing anything with the files, since any filtering of file types is easily bypassed, by accident or intentionally-

提交回复
热议问题