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

前端 未结 3 595
自闭症患者
自闭症患者 2021-01-01 09:33

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

相关标签:
3条回答
  • 2021-01-01 09:57

    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.

    0 讨论(0)
  • 2021-01-01 10:02

    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.

    0 讨论(0)
  • 2021-01-01 10:06

    more explicitly...

    <input type="file" accept="application/pdf" /> 
    
    0 讨论(0)
提交回复
热议问题