How to get the HTML's input element of “file” type to only accept pdf files?

前端 未结 7 1928
生来不讨喜
生来不讨喜 2021-01-01 08:22

is there any way that html element file


only accept PDF files and when we

7条回答
  •  别那么骄傲
    2021-01-01 09:18

    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:

    
    

    You can string together multiple mime types with a comma.

    The following string will accept JPG, PNG, GIF, PDF, and EPS files:

    
    

    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.

提交回复
热议问题