首先是下载
var request = require('request'); var fs = require('fs');
.
function downloadFile(file_url , targetPath){
// Save variable to know progress
var received_bytes = 0;
var total_bytes = 0;
var req = request({
method: 'GET', uri: file_url
});
var out = fs.createWriteStream(targetPath);
req.pipe(out);
req.on('response', function ( data ) {
// Change the total bytes value to get progress later.
total_bytes = parseInt(data.headers['content-length' ]);
});
req.on('data', function(chunk) {
// Update the received bytes
received_bytes += chunk.length;
showProgress(received_bytes, total_bytes);
});
req.on('end', function() {
console.log('下载成功')
});
}
然后是调用
// downloadFile("https://vms-ipad-h5.s3.cn-northwest-1.amazonaws.com.cn/static/media/AUDI.1dd73b80.mp4", __dirname + "/src/assets/videos/AUDI.mp4");
直接传入下载的地址,和有用的路径就行,这里我采用的是path里拿到的当前相对路径后的相对位置
要监听下载进度的函数
function showProgress(received, total){
var percentage = (received * 100) / total;
console.log(percentage + "% | " + received + " bytes out of " + total + " bytes.");
}
删除的话还没试,贴个链接
https://blog.csdn.net/qq_30100043/article/details/52979714?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control&dist_request_id=1619573273079_83767&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control