2
<input type="file" accept=".csv" /> 

Above code allows to uploading of .txt or any type of file also.

How to restrict other file types in html5?

2

3 Answers 3

2

You can't restrict other file types in html5 file input, if user select other files it allow to select. Attribute 'accept' just give hint to user about supported file types.

Sign up to request clarification or add additional context in comments.

Comments

1

With the input attribute you can specify any file extension to be uploaded. To add multiple attributes, separate them with a comma as so:

<input type="file" accept=".txt,.jpg"> 

You can also set a predefined family of extensions to be available for upload. Simply use either audio/*, video/*, or image/* to select those file types respectively. These can also be combined with regular file extensions. E.g.

<input type="file" accept="image/*,.psd"> 

This lets you upload an image or Photoshop file.

I hope this helps!

3 Comments

Still you can upload any type of file.
@Nitul Yes, but the selection dialog defaults to those file types. Checking and restricting of the files should be done client side (e.g. JavaScript), not via the HTML tag. This attribute just helps reduce the number of incorrect file types uploaded.
And server-side checks are very important as well, because a malicious user can do a custom HTTP request to bypass the client-side checks
0

Try this

<input id="fileSelect" type="file" accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" /> 

Also Check this with more details http://jsfiddle.net/dirtyd77/LzLcZ/144/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.