如何获取HTML的“文件”输入元素键入只接受PDF文件? [英] How to get the HTML's input element of "file" type to only accept pdf files?

查看:118
本文介绍了如何获取HTML的“文件”输入元素键入只接受PDF文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以让html元素文件

 < input name =file1type =filestyle = 宽度:300像素 > 

只接受PDF文件,当我们浏览它的只显示PDF文件时...



谢谢

解决方案

获取HTML 文件输入表单元素以仅接受PDF,您可以在现代浏览器中使用 accept 属性,例如Firefox 9+,Chrome 16+,Opera 11+和IE10 +例如:

< input name =file1type =fileaccept =application / pdf/>



您可以用逗号将多个MIME类型串在一起。



以下字符串将接受JPG,PNG,GIF,PDF和EPS文件:

< input name =footype =fileaccept =image / jpeg,image / gif,image / png,application / pdf,image / x-eps/>



在较旧的浏览器中,本地操作系统文件对话框不能被限制 - 您必须使用Flash或Java applet或类似的东西来处理文件传输。 / p>

当然,不用说这对于验证文件类型的有效性没有任何作用 。一旦文件上传,您就可以在服务器端执行此操作。



稍微更新 - 使用javascript和FileReader API ,您可以在将大文件上传到服务器并再次检查之前进行更多验证客户端。

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

解决方案

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.

这篇关于如何获取HTML的“文件”输入元素键入只接受PDF文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆