是一种使用Node.js编写的递归下载文件的方法,它使用了Promises和分块数组的概念来实现并发下载和断点续传功能。
概念:
优势:
应用场景:
推荐的腾讯云相关产品和产品介绍链接地址:
以下是一个示例代码,演示了如何使用带promises的分块数组的node.js递归下载:
const fs = require('fs');
const axios = require('axios');
const { promisify } = require('util');
const writeFileAsync = promisify(fs.writeFile);
async function downloadFile(url, filePath) {
try {
const response = await axios.get(url, { responseType: 'stream' });
const totalSize = response.headers['content-length'];
let downloadedSize = 0;
const writer = fs.createWriteStream(filePath);
response.data.on('data', (chunk) => {
downloadedSize += chunk.length;
const progress = (downloadedSize / totalSize) * 100;
console.log(`Downloaded: ${progress.toFixed(2)}%`);
});
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', reject);
});
} catch (error) {
throw new Error(`Failed to download file: ${error.message}`);
}
}
async function recursiveDownload(urls, directory) {
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory);
}
const promises = urls.map(async (url, index) => {
const filePath = `${directory}/file${index + 1}.txt`;
await downloadFile(url, filePath);
console.log(`File ${index + 1} downloaded successfully.`);
});
await Promise.all(promises);
console.log('All files downloaded successfully.');
}
const urls = [
'https://example.com/file1.txt',
'https://example.com/file2.txt',
'https://example.com/file3.txt'
];
const directory = 'downloads';
recursiveDownload(urls, directory)
.then(() => console.log('Download completed.'))
.catch((error) => console.error(`Download failed: ${error.message}`));
请注意,以上示例代码仅为演示目的,实际使用时需要根据具体需求进行适当修改和优化。
领取专属 10元无门槛券
手把手带您无忧上云