我能够下载文件。但是我想查找文件的下载状态,即下载是否完成(或)完成了多少%。
任何人都可以帮我解决这个问题...
我的index.js:
var express = require('express');
var router = express.Router();
var fs = require('fs');
var request = require('request');
var download = function(uri, filename, callback){
request.head(uri, function(err, res, body){
console.log('content-type:', res.headers['content-type']);
console.log('content-length:', res.headers['content-length']);
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
});
};
download('https://www.google.com/images/srpr/logo3w.png', 'google.png', function(){
console.log('done');
});
module.exports = router;
答案 0 :(得分:1)
你以前见过这个节点包吗?
https://www.npmjs.com/package/request-progress
您可以将其与请求结合使用来报告进度,如下所示:
var request = require('request');
var progress = require('request-progress');
// Note that the options argument is optional
progress(request('http://google.com/doodle.png'), {
throttle: 2000, // Throttle the progress event to 2000ms, defaults to 1000ms
delay: 1000 // Only start to emit after 1000ms delay, defaults to 0ms
})
.on('progress', function (state) {
console.log('received size in bytes', state.received);
// The properties bellow can be null if response does not contain
// the content-length header
console.log('total size in bytes', state.total);
console.log('percent', state.percent);
})
.pipe(fs.createWriteStream('doodle.png'))
希望这有帮助!