is there any way that html element file
<input name="file1" type="file" style="width:300px">
only accept PDF files and when we browse its only show PDF files...
Thanks
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!
To get the HTML file
input form element to only accept PDFs, you can use the accept
attribute in modern browsers such as Firefox 9+, Chrome 16+, Opera 11+ and IE10+ like such:
<input name="file1" type="file" accept="application/pdf">
You can string together multiple mime types with a comma.
The following string will accept JPG, PNG, GIF, PDF, and EPS files:
<input name="foo" type="file" accept="image/jpeg,image/gif,image/png,application/pdf,image/x-eps">
In older browsers the native OS file dialog cannot be restricted – you'd have to use Flash or a Java applet or something like that to handle the file transfer.
And of course it goes without saying that this doesn't do anything to verify the validity of the file type. You'll do that on the server-side once the file has uploaded.
A little update – with javascript and the FileReader API you could do more validation client-side before uploading huge files to your server and checking them again.
Not really. See File input 'accept' attribute - is it useful? .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With