当前位置:网站首页>【electron】文件下载卡住
【electron】文件下载卡住
2022-04-22 18:06:00 【雷阵太阳】
前因
遇到有极少部分电脑在下载文件时,进度条不动了,无报错。监听的end, complate,data,error,都不触发
过程
测试时发现
1、所有文件均有可能出现下载过程中莫名停止,不管文件大小,不管什么文件后缀。
2、使用http下载,没有重现。
怀疑1:是否文件服务器问题导致?
不是。因为换一个https服务器地址,测试时仍会出现。
怀疑2:是否https包有问题?
不是。不用https.get改用request的方式请求,测试时仍会出现。
怀疑3:是否超时?
是。但为何监听不到?因为默认超时时间好像是2分钟吧。
一方面代码没设置时长,一方面测试时从未等待到2分钟以上,误以为是卡住了。
后续因为验证怀疑2时把https.get改成request了,就在request的基础上加上超时限制。
const req = request({
method: 'GET',
uri: mfileDownUrl,
timeout: 2000
});
req.on('error', function(e) {
console.error('[error]: ' + e);
});
req.on('abort', function() {
console.log('abort...');
});
设置超时时间,会先触发abort,然后触发error收到Error: ESOCKETTIMEDOUT
参考 electron使用nodejs实现文件流式下载并显示进度
参考 http.get 怎么捕获timeout(网络超时)事件?
结论
文件下载未考虑超时的情况导致的。
https.get使用不熟练,知道超时又监听不到,监听到了中止不了。
最后改用request,设置timeout,触发error收到Error: ESOCKETTIMEDOUT事件时是超时。
中止文件下载使用req.abort()
建议下载文件时一定要带上的监听几件套
const req = request({
method: 'GET',
uri: mfileDownUrl,
timeout: 2000//可以设置大点,这里是因为在测试
});
mfile = fs.createWriteStream(mfileFullName);
req.pipe(mfile);
req.on('error', function(e) {
console.error('[error]: ' + e);
});
req.on('abort', function() {
console.log('abort...');
});
req.on('response', (data) => {
// 更新总文件字节大小
})
req.on('data', (chunk) => {
//更新进度条
});
req.on('end', () => {
console.log('下载已完成,等待处理');
});
版权声明
本文为[雷阵太阳]所创,转载请带上原文链接,感谢
https://blog.csdn.net/sinat_29158831/article/details/108124826
边栏推荐
- 国产芯片DP9637-K总线收发器替代L9637D芯片和SI9241
- 【思考与进步】:关于自己的遗憾
- 数字化靶场的未来方向
- What data indicators should app focus on?
- Soft test high-level notes | tools and techniques for collecting needs
- Soft test high item notes | contents of project proposal
- 【C语言进阶篇】一篇文章带你认清结构、枚举和联合
- 接口协议之抓包分析 TCP 协议
- Read and write txt files in C language
- SAP UI5 数据类型(data type) 学习笔记
猜你喜欢

【C语言进阶篇】一篇文章带你认清结构、枚举和联合

软考高项笔记 | 运维服务分类

国产芯片DP9637-K总线收发器替代L9637D芯片和SI9241

Activiti 挂起与激活任务

Soft test advanced item notes | soft skills

Soft test high item notes | typical life cycle model of information system project

软考高项笔记 | 信息系统安全

软考高项笔记 | 软技能

An understanding process and its communication method

秒云助力中电科32所发布“基于拟态应用集成框架的SaaS云管理平台解决方案”
随机推荐
贵医通预约挂号
An idea plug-in that doesn't work, but can install X
Huawei router realizes the connection between headquarters and branches through MPLS virtual private network
How to quickly switch and display different software interfaces on the computer
这个API Hub厉害了,收录了钉钉企业微信等开放Api,还能直接调试!
软考高项笔记 | 项目的特点
What data indicators should app focus on?
『程序员延寿指南』:跟着码农干,多活20年!
rm格式的文件怎么打开?
软考高项笔记 | 详细可行性研究的内容
Soft test advanced item notes | software integration technology
Topic5——198. raid homes and plunder houses
How do I completely delete files on my computer?
Notes on soft test high items | steps of feasibility study
PiKachu-File Inclusion(文件包含漏洞)
软考高项笔记 | 运维服务分类
Common error reporting records
一个没啥L用,但可以装X的IDEA插件
【Lane】Ultra-Fast-Lane-Detection(1)自定义数据集训练
手机运行内存应该怎么选?