如何循环同步下载文件

1.下载同步下载代码,请求超时时间30秒:

复制代码
async function downloadFile(url, filePath, timeoutMs = 30000) {
            return new Promise((resolve, reject) => {
                // 创建超时定时器
                const timeout = setTimeout(() => {
                    reject(new Error(`下载超时 (${timeoutMs}ms)`));
                }, timeoutMs);

                const request = https.get(url, (response) => {
                    const fileStream = fs.createWriteStream(filePath);
                    
                    response.pipe(fileStream);
                    
                    fileStream.on('finish', () => {
                        clearTimeout(timeout); // 清除超时定时器
                        fileStream.close(resolve); // 关闭文件流并解析Promise
                    });
                    
                    fileStream.on('error', (err) => {
                        clearTimeout(timeout);
                        fs.unlink(filePath, () => {}); // 删除文件
                        reject(err);
                    });
                    
                    response.on('error', (err) => {
                        clearTimeout(timeout);
                        fs.unlink(filePath, () => {});
                        reject(err);
                    });
                });

                request.on('error', (err) => {
                    clearTimeout(timeout);
                    fs.unlink(filePath, () => {});
                    reject(err);
                });

                // 确保在Promise结束时清除超时
                Promise.resolve().then(() => {}).catch(() => {}).finally(() => {
                    clearTimeout(timeout);
                });
            });
        }

2.组装文件全路径,判断是否存在该文件,若不存在就下载:

复制代码
async function downloadItemFile(url, directoryPath, id, is_photo) {
            let fileName = path.basename(url); // 获取文件名
            if(is_photo){
                fileName = fileName + '.jpeg';
            }else{
                fileName = fileName + '.mp4';
            }
            const filePath = path.join(directoryPath, fileName); // 获取文件保存路径
            if(!files.includes(fileName)){
                logger.debug('url:', url, 'fileName:', fileName,  'propertyListing.outh_id:', id);
                list1.push(url);
                fs.mkdir(directoryPath, { recursive: true }, (err) => {
                    if (err) {
                        console.error(`Error creating folder ${directoryPath}:${err.message}`);
                    } else {
                        console.log(`Folder ${directoryPath} created`);
                    }
                }); 
                await downloadFile(url, filePath);
            }
        }

3.循环一个文件一个文件的下载:

await downloadItemFile(item.url, directoryPath1, propertyListing.out_id, false);和await downloadFile(url, filePath);注意:一定要加await不然就变成同步下载了。若有多个文件需要下载就会一次建立多个http请求去异步下载,会出现图片和视频下载不完整的问题。

复制代码
        for(let i = 0; (i < list.length) && (i < m); i++){
            let propertyListing = list[i];
            if(Tool.checkNotEmptString(propertyListing.out_id)){
                // if(Tool.checkNotEmptArray(propertyListing.photos)){
                //     for(let photo of propertyListing.photos){
                //         if(Tool.checkNotEmptString(photo.url)){
                //             downloadItemFile(photo.url, directoryPath1, propertyListing.id, true);
                //         }      
                //     }
                // }else 
                if(Tool.checkNotEmptArray(propertyListing.videos)){
                    for(let item of propertyListing.videos){
                        if(Tool.checkNotEmptString(item.url)){
                            await downloadItemFile(item.url, directoryPath1, propertyListing.out_id, false);
                        }      
                    }
                }
            }

        }
相关推荐
q***42052 分钟前
PHP使用Redis实战实录2:Redis扩展方法和PHP连接Redis的多种方案
开发语言·redis·php
EndingCoder31 分钟前
Node.js 常见安全漏洞防范
node.js·sql注入·安全漏洞·csrf防护·ai内容安全
qq_433554541 小时前
C++ 稀疏表
开发语言·c++·算法
z***y8621 小时前
Java数据挖掘开发
java·开发语言·数据挖掘
zeijiershuai1 小时前
Vue 工程化、ElementPlus 快速入门、ElementPlus 常见组件-表格组件、ElementPlus常见组件-分页条组件
前端·javascript·vue.js
软件开发技术深度爱好者2 小时前
Python库/包/模块管理工具
开发语言·python
bubiyoushang8882 小时前
基于MATLAB的自然图像梯度分布重尾特性验证方案
开发语言·matlab
漫天黄叶远飞2 小时前
把原型链画成地铁图:坐 3 站路就能看懂 JS 的“继承”怎么跑
前端·javascript
人工智能训练2 小时前
前端框架选型破局指南:Vue、React、Next.js 从差异到落地全解析
运维·javascript·人工智能·前端框架·vue·react·next.js
MSTcheng.2 小时前
【C++STL】priority_queue 模拟实现与仿函数实战
开发语言·c++