如何限制以便它只能选择.pdf文件?

时间:2021-10-14 07:35:25

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

默认情况下,它可以选择所有类型的文件,如何限制它,以便它只能选择.pdf文件?

3 个解决方案

#1


27  

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.

您可以使用表单上的accept属性向浏览器建议限制某些类型。但是,您需要在服务器端代码中重新验证以确保。永远不要相信客户送你的东西。

#2


73  

more explicitly...

更明确地......

<input type="file" accept="application/pdf" /> 

#3


1  

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.

简单地说:你不能使用普通的html和javascript。您可以获得的最接近的是在提交表单之前使用javascript测试文件扩展名,并向用户显示一些错误消息(如果它不是.pdf)。如果要实现此目的,您可能需要使用某些客户端解决方案,例如Flash上​​载控件。

#1


27  

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.

您可以使用表单上的accept属性向浏览器建议限制某些类型。但是,您需要在服务器端代码中重新验证以确保。永远不要相信客户送你的东西。

#2


73  

more explicitly...

更明确地......

<input type="file" accept="application/pdf" /> 

#3


1  

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.

简单地说:你不能使用普通的html和javascript。您可以获得的最接近的是在提交表单之前使用javascript测试文件扩展名,并向用户显示一些错误消息(如果它不是.pdf)。如果要实现此目的,您可能需要使用某些客户端解决方案,例如Flash上​​载控件。