Filter files in HTML file open dialog

泄露秘密 提交于 2019-12-01 09:10:01

Standard

Actually in HTML5 you can set the accept attribute so now this is possible! The W3C standard states:

The accept attribute may be specified to provide user agents with a hint of what file types will be accepted.

Accepted values [Full list in Wikipedia]

Just pass a valid MIME type to the attribute for example:

  • audio/*
  • video/*
    • video/ogg
  • image/*
    • image/jpg
    • image/bmp

Example code

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

my question is, can i filter the files by passing the extension filter to that dialog?

No you can't do this with the plain type="file" input. You could use some Flash upload controls though that allow you to achieve this.

I don't think you can edit this dialog options, but you can validate the file after the user select it.

tO edit the dialog, I remember that you can do that by a flash or Silverlight uploaders, such as swfUpload.

Here is a piece of code that I came up with when I was in need of filtering some of the file types:

<input type="file" accept=".xls, .xlsx, .csv" placeholder="File to be upload" />

Accept attribute supports multiple files comma separated.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!