html5 上传压缩文件类型限制解决方案
2018-02-27 16:41
1061 查看
需求场景:利用input 上传文件,限制文件类型为zip,或者rar压缩包类型。
①尝试通过accept限制,发现除了zip和rar文件,excel,doc等文件也能选择。
②于是,解决方案是通过对文件名的判断,增加一层过滤,
代码如下html代码:au-upload(@change="changeCompress",accept="application/x-zip-compressed,application/x-rar-compressed",style="margin-bottom:5px;margin-left:5px",:loading="loading",ref="uploadCompress",button-text="选择压缩包")js代码://压缩包选择改变changeCompress($event) { console.log('changeCompress'); let files = $event.target.files || $event.dataTransfer.files; if (!files.length) { Aurora.warning('请选择文件'); return; }
let file = files[0]; //文件类型控制 let fileName = file.name; let pos = fileName.lastIndexOf("."); let lastName = fileName.substring(pos, fileName.length); if ((lastName.toLowerCase() != ".zip" ) && (lastName.toLowerCase() != ".rar")) { Aurora.warning("文件必须为.zip或.rar类型"); this.resetCompressData(); return; }
if (file.size > 1024 * 1024 * 20) { Aurora.warning('文件不能大于20M'); this.resetCompressData(); return; } this.form.compressFile = file;},
①尝试通过accept限制,发现除了zip和rar文件,excel,doc等文件也能选择。
②于是,解决方案是通过对文件名的判断,增加一层过滤,
代码如下html代码:au-upload(@change="changeCompress",accept="application/x-zip-compressed,application/x-rar-compressed",style="margin-bottom:5px;margin-left:5px",:loading="loading",ref="uploadCompress",button-text="选择压缩包")js代码://压缩包选择改变changeCompress($event) { console.log('changeCompress'); let files = $event.target.files || $event.dataTransfer.files; if (!files.length) { Aurora.warning('请选择文件'); return; }
let file = files[0]; //文件类型控制 let fileName = file.name; let pos = fileName.lastIndexOf("."); let lastName = fileName.substring(pos, fileName.length); if ((lastName.toLowerCase() != ".zip" ) && (lastName.toLowerCase() != ".rar")) { Aurora.warning("文件必须为.zip或.rar类型"); this.resetCompressData(); return; }
if (file.size > 1024 * 1024 * 20) { Aurora.warning('文件不能大于20M'); this.resetCompressData(); return; } this.form.compressFile = file;},
相关文章推荐
- HTML5:input file控件限制上传文件类型
- html5 中input type=‘file’实现文件图片等的上传的文件类型限制
- HTML5:input file控件限制上传文件类型
- Apache+PHP,swfupload做批量上传时遇到的文件类型支持问题和文件上传大小限制问题的相应解决方案
- SpringMVC全局文件上传类型限制【终极解决方案】
- Struts2文件上传 —— 限制文件的大小及类型
- ASP.Net 文件上传大小限制解决方案修改IIS7/7.5配置
- struts 1 中服务器端限制文件上传的类型及扩展名
- Win2008或IIS7的文件上传大小限制解决方案,修改IIS7/7.5配置——资料总结
- j2ee安全问题(springmvc xss 文件上传类型限制 resin banner修改)
- struts2上传文件类型限制
- Uploadify 3.2上传文件,限制类型,大小,传递参数等
- Struts2 使用拦截器限制上传文件类型
- Struts2 上传文件类型限制(zip和rar)
- php限制上传文件类型并保存上传文件的方法
- ASP.Net 文件上传大小限制解决方案修改IIS7/7.5配置
- MOSS 2007应用日记(23)――如何修改上传文件大小及类型的限制
- html 5 如何限制上传的文件类型 (uploadifive)
- Windows server 2008修改IIS7的文件上传大小限制解决方案
- 限制上传文件类型的两种方法