Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter input type="file" dialog by specific file type?

I want to restrict the browser to JPG files when I click on browse button of the <input type="file">.

Is it possible to browse for specific file types?

like image 687
Sachin J Avatar asked Jan 20 '12 07:01

Sachin J


People also ask

How do I filter search by file type?

You can use the filetype: operator in Google Search to limit results to a specific file type. For example, filetype:rtf galway will search for RTF files with the term "galway" in them.

How do I only accept certain file types in HTML?

Acceptable file types can be specified with the accept attribute, which takes a comma-separated list of allowed file extensions or MIME types. Some examples: accept="image/png" or accept=". png" — Accepts PNG files.

How do I select an input type file?

The <input type="file"> defines a file-select field and a "Browse" button for file uploads. To define a file-select field that allows multiple files to be selected, add the multiple attribute. Tip: Always add the <label> tag for best accessibility practices!

How do I restrict the file upload in react?

You can restrict the maximum allowed file size (in bytes) by using the maxFileSize property. If the selected file exceeds the maximum size, an error message will be displayed.


2 Answers

This will give the correct (custom) filter when the file dialog is showing:

<input type="file" accept=".jpg, .png, .jpeg, .gif, .bmp, .tif, .tiff|image/*"> 
like image 121
Vegard Avatar answered Nov 13 '22 16:11

Vegard


See http://www.w3schools.com/tags/att_input_accept.asp:

The accept attribute is supported in all major browsers, except Internet Explorer and Safari. Definition and Usage

The accept attribute specifies the types of files that the server accepts (that can be submitted through a file upload).

Note: The accept attribute can only be used with <input type="file">.

Tip: Do not use this attribute as a validation tool. File uploads should be validated on the server.

Syntax <input accept="audio/*|video/*|image/*|MIME_type" />

Tip: To specify more than one value, separate the values with a comma (e.g. <input accept="audio/*,video/*,image/*" />.

like image 44
JB Nizet Avatar answered Nov 13 '22 16:11

JB Nizet