如何在nodejs中下载文件( http和https)并实时查看/显示下载进度?

您所在的位置:网站首页 nodejs文档下载 如何在nodejs中下载文件( http和https)并实时查看/显示下载进度?

如何在nodejs中下载文件( http和https)并实时查看/显示下载进度?

2023-08-15 06:40| 来源: 网络整理| 查看: 265

您可以使用axios模块,您可以传递URL,它将处理一个协议http或https。请参见下面的代码

'use strict' const Fs = require('fs') const Path = require('path') const Axios = require('axios') const ProgressBar = require('progress') async function downloadImage () { const url = 'http://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_5mb.mp4' console.log('Connecting …') const { data, headers } = await Axios({ url, method: 'GET', responseType: 'stream' }) const totalLength = headers['content-length'] console.log('Starting download') const progressBar = new ProgressBar('-> downloading [:bar] :percent :etas', { width: 40, complete: '=', incomplete: ' ', renderThrottle: 1, total: parseInt(totalLength) }) const writer = Fs.createWriteStream( Path.resolve(__dirname, '', 'test.mp4') ) data.on('data', (chunk) => { progressBar.tick(chunk.length) console.log("% complted",(progressBar.curr/totalLength)*100) }) data.pipe(writer) } downloadImage()

来源:https://futurestud.io/tutorials/axios-download-progress-in-node-js

演示:https://repl.it/repls/FluffyAmpleArrays



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3