Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict <input type="file"> so that it can only select .pdf files?

Tags:

html

file-io

pdf

By default it can select all type of files,how to restrict it so that it can only select .pdf files?

like image 777
fms Avatar asked Jan 08 '11 13:01

fms


People also ask

How do you specify a file type in input?

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 accept a PDF input?

Just insert an onclick attribute on your submit button and call the code that will test the input file value. If the extension is forbidden, you'll have to return false to invalidate the form.


3 Answers

more explicitly...

<input type="file" accept="application/pdf" /> 
like image 74
Timmerz Avatar answered Sep 24 '22 14:09

Timmerz


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.

like image 30
David Avatar answered Sep 25 '22 14:09

David


Simply put: you can't using the plain html and javascript. The closest you can get is to test the file extension using javascript before submitting the form and show some error message to the user if it is other than .pdf. You might need to use some client side solution such as Flash upload controls if you want to achieve this.

like image 40
Darin Dimitrov Avatar answered Sep 24 '22 14:09

Darin Dimitrov