解决input[type=file]打开时慢、卡顿问题
作者:魔力叉烧包
链接:https://segmentfault.com/a/1190000010397414
昨天临下班测试给我问我为什么图片上传插件打开文件夹的速度这么慢,让我想办法优化一下
然后我就努力的搞了起来_(:з」∠)_
由于我们内部系统不兼容ie,所以我就没有管ie,在浏览器里面玩了起来
经过测试发现,在mac里面safari、Firefox、Chrome(opera不知道为啥老闪退)都没有卡顿问题
在windows里面,Firefox不卡顿,只有Chrome卡顿。
然而,这个插件是从另一个项目里面借用过来,再加上了限定图片类型的功能而已。
原组件并没有这个卡顿问题,那么问题只可能是在限定图片类型这点上了。
先贴上我的代码
<input
accpet="image/*"
style={inputStyle}
ref={c=> this._imgFile = c}
onChange={this.handleChange.bind(this)}
type="file" name="image" disabled={disabled}
/>
于是我决定先去掉accpet试试……
果然就没有了卡顿的问题。
那么本包在试试accpet=”image/jpg”果然也不卡卡的了!!
看来问题的所在就是”image/*”
但是写accpet的原意是要想要筛选出所有图片_(:з」∠)_
那么为了实现这个需求,同时提高用户体验,只能采取枚举了
修改后的代码
<input
style={inputStyle}
ref={c=> this._imgFile = c}
onChange={this.handleChange.bind(this)}
type="file" name="image" disabled={disabled}
accpet="image/gif,image/png,image/jpeg,image/jpg,image/bmp"
/>
再试试,果然妥妥的了!
但是到底是为什么会这么卡呢??我查了查万能的Stack Overflow→_→
原来是因为Chrome的SafeBrowsing功能会在上传或保存时检查文件,
如果网络连接到google的速度比较快呢,就没有什么问题。
但是如果连接比较慢,或者干脆跪掉了,那SafeBrowsing就会让Chrome挂起一段时间,直到文件检查结束或者超时
使用 accept="image/png, image/jpeg, image/gif"
就可以解决这个问题,因为这些MIME类型在SafeBrowsing的白名单里面,不需要检查。
但是如果用像是 accept="image/*"
这样的呢,就不行了,就有可能变得卡卡的。
●编号1049,输入编号直达本文
●输入m获取文章目录
Web开发
- 解决input[type=file]打开时慢、卡顿问题
- 解决input[type=file]打开时慢、卡顿问题
- 解决input[type=file]打开时慢、卡顿问题
- 解决JS(Vue)input[type='file'] change事件无法上传相同文件的问题
- .net 2.0 中对于非服务器端的input元素 type为file后台无法获取的问题的解决方法
- 解决在高版本浏览器下的input type="file"的路径问题
- 解决ie10及其以下不兼容input type=file的问题
- input type="file" 选取文件路径时出现fakepath问题IE浏览器解决办法
- 解决<input type=“file” multiple> 多文件上传问题(可以多次选取文件,也可以一次选取多个文件),而且点击的是一个同一个div
- chrome浏览器input[type=file]选择zip、exe等文件卡顿解决办法
- apktool 反编译 Input file was not found or was not readable.问题解决方案
- type为file的input控件文件上传打开缓慢
- stm32f4xx_it.c: Error: C3065E: type of input file 'DRIVER' unknown 解决办法
- stm32f10x_it.c: Error: C3065E: type of input file 'and' unknown 错误解决
- Cocos2d-x 3.x 中解决 无法打开包括文件:“json/document.h”: No such file or directory 的问题
- input标签 type='file' 上传图片问题
- 解决:mount: unknown filesystem type ‘ntfs’ 问题
- input标签 type='file' 上传图片问题
- hadoop "File /user/<user>/input/conf/slaves could only be replicated to 0 nodes, instead of 1"问题及解决办
- 问题解决:fatal error C1083: 无法打开包括文件:No such file or directory