如何使输入类型=文件应该只接受pdf和xls [英] How to make input type= file Should accept only pdf and xls
问题描述
我用< input type =filename =Upload>
只想接受.pdf和.xls文件。
Now I would like to restrict this by accepting only .pdf and .xls files.
当我点击提交按钮时,它应该验证这一点。
When I click the submit button it should validate this.
当我点击网页上的文件(PDF / XLS)时,它应该会自动打开。
And when I click the files (PDF/XLS) on webpage it should automatically open.
任何人都可以举一些例子吗?你可以通过使用属性 accept
并添加允许的mime-id来完成这项工作。
Could anyone please give some examples for this?
推荐答案
类型。但并非所有浏览器都尊重该属性,并且可以通过代码检查器轻松删除。因此无论哪种情况,您都需要检查服务器端的文件类型(第二个问题)。
You could do so by using the attribute accept
and adding allowed mime-types to it. But not all browsers do respect that attribute and it could easily be removed via some code inspector. So in either case you need to check the file type on the server side (your second question).
示例:
Example:
<input type="file" name="upload" accept="application/pdf,application/vnd.ms-excel" />
到第三个问题当我点击网页上的文件(PDF / XLS)时,打开。:
To your third question "And when I click the files (PDF/XLS) on webpage it automatically should open.":
你无法做到这一点。如何在客户机上打开PDF或XLS由用户设置。
You can't achieve that. How a PDF or XLS is opened on the client machine is set by the user.
这篇关于如何使输入类型=文件应该只接受pdf和xls的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!