132

I want my uploader only allows these types:

  • doc, docx.
  • xls, xlsx.
  • ppt, pptx.
  • txt.
  • pdf.
  • Image types.

How can I achieve this? What should I put in the accept attribute? Thanks for your help.

EDIT!!!

I have one more thing to ask. When the popup appears for use to choose file, at the down right corner, there is a drop down list contains all allow files. In my case, the list would be long. I see in the list, there is an option called All Supported Types. How can I make it chosen by default and eliminate all other options?

Any help will be appreciated. Thank you.

3
  • Will this help? stackoverflow.com/questions/11601342/…. Not sure if "application/doc", "application/pdf", and "application/ppt" mime types are supported. Commented Jun 25, 2013 at 9:41
  • 2
    Possible duplicate of Limit file format when using <input type="file">? Commented Jul 27, 2017 at 15:09
  • Regarding your 2nd question: "The accept attribute doesn't validate the types of the selected files; it simply provides hints for browsers to guide users towards selecting the correct file types. It is still possible (in most cases) for users to toggle an option in the file chooser that makes it possible to override this and select any file they wish, and then choose incorrect file types. Because of this, you should make sure that the accept attribute is backed up by appropriate server-side validation." developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file Commented Jul 18, 2018 at 20:17

9 Answers 9

195

The value of the accept attribute is, as per HTML5 LC, a comma-separated list of items, each of which is a specific media type like image/gif, or a notation like image/* that refers to all image types, or a filename extension like .gif. IE 10+ and Chrome support all of these, whereas Firefox does not support the extensions. Thus, the safest way is to use media types and notations like image/*, in this case

<input type="file" name="foo" accept=
"application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint,
text/plain, application/pdf, image/*">

if I understand the intents correctly. Beware that browsers might not recognize the media type names exactly as specified in the authoritative registry, so some testing is needed.

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

7 Comments

This will not work if your doc file made with ubuntu software.
@Jukka K. Korpela that filter gets me all the office files i want,thank you
To add suport for docx, xlsx and pptx: application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.openxmlformats-officedocument.presentationml.slideshow
Maybe it was true in 2014 when this answer was given, but I can confirm that a comma separated list of extensions is working correctly on Firefox.
did not work when try to upload from android/ios device. If anybody has a solution let me know.
|
144

Use Like below

<input type="file" accept=".xlsx,.xls,image/*,.doc, .docx,.ppt, .pptx,.txt,.pdf" />

1 Comment

Browsers usually do not check the file content, so extension is ok - even preferred since browsers do not know all the mime types.. You have to check the file content yourself.
14

IMPORTANT UPDATE:

Due to use of only application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint... allows only till 2003 MS products, and not newest. I've found this:

application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/vnd.ms-powerpoint, application/vnd.openxmlformats-officedocument.presentationml.slideshow, application/vnd.openxmlformats-officedocument.presentationml.presentation

And that includes the new ones. For other files, you can retrieve the MIME TYPE in your file by this way (pardon the lang)(in MIME list types, there aren't this ones):

enter image description here

You can select & copy the type of content

1 Comment

why not to just write extensions?
13

Use accept attribute with the MIME_type as values

<input type="file" accept="image/gif, image/jpeg" />

3 Comments

The code given accepts only GIF and JPEG files, as opposite to what was requested for in the question.
@JukkaK.Korpela, This is just sample code and you can set the accept attribute with the MIME_types as values
This still presents an "All Files" option.
9

for powerpoint and pdf files:

<html>
<input type="file" placeholder="Do you have a .ppt?" name="pptfile" id="pptfile" accept="application/pdf,application/vnd.ms-powerpoint,application/vnd.openxmlformats-officedocument.presentationml.slideshow,application/vnd.openxmlformats-officedocument.presentationml.presentation"/>
</html>

Comments

6

As stated on w3schools:

audio/* - All sound files are accepted

video/* - All video files are accepted

image/* - All image files are accepted

MIME_type - A valid MIME type, with no parameters. Look at IANA MIME types for a complete list of standard MIME types

Comments

0

This is the complete list of MIME Type formats according to extension.

  • .doc: application/msword
  • .docx: application/vnd.openxmlformats-officedocument.wordprocessingml.document
  • .json: application/json
  • .txt: text/plain
  • ...

Example:

<input type="file" accept='application/msword, text/plain' />

Comments

-1
<input type=file accept=".jpg, .pdf, .xls">

This is a solution for accepting values.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-2

for image write this

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

For other, You can use the accept attribute on your form to suggest to the browser to restrict certain types. However, you'll want to re-validate in your server-side code to make sure. Never trust what the client sends you

1 Comment

The code given accepts only image files, as opposite to what was requested for in the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.