<input type="file" />浏览时只显示指定文件类型

如何通过HTML5的accept属性筛选文件上传类型
本文详细介绍了HTML5中使用accept属性来限制文件上传类型的方法,包括如何实现单一类型筛选、多种类型筛选及特殊类型的筛选,并探讨了不同浏览器的兼容性问题。此外,还解释了accept属性的并集排序规则以及与文件类型下拉框的交互方式。

<input type="file" />浏览时只显示指定文件类型

<input type="file" accept="application/msword" ><br><br>accept属性列表<br> 

1.accept="application/msexcel"
2.accept="application/msword"
3.accept="application/pdf"
4.accept="application/poscript"
5.accept="application/rtf"
6.accept="application/x-zip-compressed"
7.accept="audio/basic"
8.accept="audio/x-aiff"
9.accept="audio/x-mpeg"
10.accept="audio/x-pn/realaudio"
11.accept="audio/x-waw"
12.accept="image/gif"
13.accept="image/jpeg"
14.accept="image/tiff"
15.accept="image/x-ms-bmp"
16.accept="image/x-photo-cd"
17.accept="image/x-png"
18.accept="image/x-portablebitmap"
19.accept="image/x-portable-greymap"
20.accept="image/x-portable-pixmap"
21.accept="image/x-rgb"
22.accept="text/html"
23.accept="text/plain"
24.accept="video/quicktime"
25.accept="video/x-mpeg2"
26.accept="video/x-msvideo"

这个就可以实现选择具体的数据类型,但是有兼容问题,我在测试的时候只有Opera,Chrome能用,火狐和IE都不兼容

好吧,就在这里测试一下吧:

我只想要word

在这里如果想支持多种类型的话,比如金山的office和microsoft的office的不同扩展名,这样的话可以在accept里面放置多个属性就可以了:

<form> <input type="file" name="pic" id="pic" accept="image/gif, image/jpeg" /></form>

如果不限制图像的格式,可以写为:accept="image/*"。同样是可以的

好的,测试一下:

我只想要gif、bmp

好吧,只要是图片我就要了

嗯,测试成功,用Opera,Chrome能用,因为accept也是html5的新特性,所以火狐和IE的支持就显得单薄了,这样还是让我们等待他们的接受把
测试了好久,发现gif和bmp一起的时候并不是能取得他们的并集,仔细观察以后是按写入顺序排列了一下他们的“文件选择类型”:

就像是下面的图片一样:

我们如果不定义input file的格式的时候,那么文件类型的地方就是全部文件*.*,如果我们定义了自己的,那么文件类型就会和我们选中的一样,但是这时候我们如果想看别的文件的时候还是可以通过文件类型的下拉框,去选择自己额外喜欢的类型的,简单的说,它就是起到了一个简单的按照我们的主观意愿筛选的作用,同时我们如果不是选择的image/*而是好几个并列的话就会按照上面的情况分别得排列出来,比如图上的我input就是这个样子的:

<form> <input type="file" name="pic" id="pic" accept="image/gif, image/x-ms-bmp, image/bmp" /></form>

<!DOCTYPE html> <html> <head> <meta charset="gb2312"> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"> <title>智能蓄电池监控系统</title> <style> body{font-size:11pt;} a{text-decoration:none;} </style> </head> <body> <base target="_blank"> <script type="text/javascript"> function validate() { var psd = document.getElementById("psd").value; var name = document.getElementById("name").value; var submitOK = "true"; if (name != "Admin") { alert("用户名错误!"); submitOK = "false"; } if (psd != "111111") { alert("密码错误!"); submitOK = "false"; } if (submitOK == "false") { return false; } } </script> <span style="color:#000FFF;font-size:24px">智能蓄电池监控系统</span> <p align="right" style="margin-top:0px;margin-bottom:0px;"> <a href="/E.html">English</a> <a href="/F.html">中文</a> </p> <p align="right" style="margin-top:0px;margin-bottom:0px;">M%Num-V%Num-I%Num-P%Num</p> <hr size="1"> <br> <br> <br> <form action="/9.html" method="post" onsubmit="return validate()"> <table style="border:2px solid;" bgcolor="#f0ffff" width="500" height="200" border="0" cellpadding="5" cellspacing="0"> <tr> <td height="30%" align="center" colspan="2" style="font-size:25px;font-weight: bold;line-height:1;padding-top:20px;">用户登录</td> </tr> <tr> <td height="20%" width="35%" align="right">用户:</td> <td align="left"><input type="text" name="uname" value="Admin" id="name" size="20"></td> </tr> <tr> <td height="20%" align="right">密码:</td> <td align="left"><input type="password" name="psd" id="psd" size="20"></td> </tr> <tr> <td height="30%" align="center" colspan="2" style="padding-bottom:25px;"> <input type="submit" value="确定">   <input type="reset" value="取消"> </td> </tr> <tr> <td height="30%" colspan="2" style="font-size:15px; font-weight: normal; line-height:1; text-align:center; color:red; padding-bottom:15px;">请使用IE7浏览器兼容模式浏览使用否则可能出现乱码!</td> </tr> <tr> <td height="30%" colspan="2" style="font-size:15px; font-weight: normal; line-height:1; text-align:center; color:red; padding-bottom:15px;">如有需要可以联系原厂解决。</td> </tr> </table> <input type="hidden" name="Version" value="%Num.%Num.%Num"> </form> </body> </html>转化为C语言数据存储const unsigned char log_inpage[] = {};
09-19
<!DOCTYPE html> <html> <head> <title>文件传输系统</title> <style> .container { max-width: 600px; margin: 20px auto; padding: 20px; } .form-group { margin-bottom: 15px; } button { padding: 8px 15px; background: #4CAF50; color: white; border: none; cursor: pointer; } </style> </head> <body> <div class="container"> <!-- 文件上传区域 --> <div class="form-group"> <h2>文件上传</h2> <input type="file" id="fileInput"> <button onclick="uploadFile()">上传文件</button> <div id="uploadStatus"></div> </div> <!-- 文件下载区域 --> <div class="form-group"> <h2>文件下载</h2> <input type="text" id="filenameInput" placeholder="输入文件名"> <button onclick="downloadFile()">下载文件</button> <div id="downloadStatus"></div> </div> </div> <script> // 文件上传函数 async function uploadFile() { const fileInput = document.getElementById('fileInput'); const statusDiv = document.getElementById('uploadStatus'); if (!fileInput.files[0]) { statusDiv.innerHTML = '<span style="color:red">请文件</span>'; return; } const formData = new FormData(); formData.append('file', fileInput.files[0]); try { const response = await fetch('/upload', { method: 'POST', body: formData }); const result = await response.json(); if (result.success) { statusDiv.innerHTML = `✓ 上传成功: ${result.filename}`; } else { statusDiv.innerHTML = `✗ 上传失败: ${result.message}`; } } catch (error) { statusDiv.innerHTML = `✗ 网络错误: ${error.message}`; } } // 文件下载函数 function downloadFile() { const filename = document.getElementById('filenameInput').value; if (!filename) { document.getElementById('downloadStatus').innerHTML = '<span style="color:red">请输入文件名</span>'; return; } window.open(`/download?filename=${encodeURIComponent(filename)}`, '_blank'); } </script> </body> </html> 乱码
10-27
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值