adesh
adesh

Reputation: 852

Can I use accept attribute of html input element to specify the type of file is to be uploaded?

hello everyone i want validate the type of file being uploaded on sever to .txt extension. Can i use accept attribute of input element to filter out the type of file. If yes how can i do that and if any other way ??

Some code is

   <input type="file"     name="uploadfile" value="" size="50" /> 

Thanks

Upvotes: 1

Views: 1644

Answers (3)

alexsuslin
alexsuslin

Reputation: 4224

here is javascript custom validation

var files = document.getElementsByName("uploadfile");
if(files[0].value.indexOf(".txt") == -1) {
// Code placed here will be executed if an invalid file type is found.
}

And yes you can use accept attribute as well:

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

However as far as I know it is not supported by IE and Safari

But please make sure to validate the uploaded file on the server side as well. Just client validation is not enough.

Upvotes: 3

Jukka K. Korpela
Jukka K. Korpela

Reputation: 201846

Yes, you can use the accept attribute. Browser supports varies. It takes an Internet media type (MIME type) list as value. For plain text (which is that .txt files usually are), that would be accept="text/plain". This may or may not affect the file selection functionality by restricting the choices to files that the browser regards as plain text files.

Upvotes: 0

Akhil Thayyil
Akhil Thayyil

Reputation: 9413

Hi in normal html you can't do that , you need flash support for this .

Better use the uploadify plugin

http://www.uploadify.com/

Upvotes: 0

Related Questions