在我的Meteor服务器应用程序中,我使用以下代码从Google下载一个文件,
var dest = fs.createWriteStream('/data/'+data.name);
drive.files.get({
fileId: data.id,
alt: 'media',
auth: jwtClient
})
.on('end', Meteor.bindEnvironment(function() {
}))
.on('error', function(err) {
console.log('Error during download', err);
})
.pipe(dest);如何获得下载进度?例如,我希望每30秒使用console.log()显示下载进度
我可以使用.on('data')吗?我使用谷歌提供的v3。
发布于 2017-04-11 09:42:56
您可以使用文件名从drive.files.list获取文件元(id、name、size),然后可以下载该文件。
使用用于谷歌驱动器的Node.js快速启动进行身份验证。
我正在使用进度流来测量接收到的%数据。
var callAfterDownload = function (fileName, callback) {
drive.files.list({
auth: oauth2Client,
pageSize: 1,
q: 'name=\'' + fileName + '\'',
fields: 'nextPageToken, files(id, name, size)'
}, function (err, response) {
if (err) {
console.log('The API returned an error: ' + err)
callback(['Error while download'])
} else {
var files = response.files
//when only one file is matched we will download
if (files.length === 1) {
var file = files.pop()
console.log('%s (%s)', file.name, file.id)
var dest = fs.createWriteStream(file.name)
var progress = Progress({time:100, length: file.size})
//downloading matched file from drive
drive.files.get({
auth: oauth2Client,
fileId: file.id,
alt: 'media'
}).on('error', function (err) {
console.log('Error during download', err)
callback(['Error while download'])
}).pipe(progress).pipe(dest)
//checking progress of file
progress.on('progress', function(progress) {
console.log('download completed ' +progress.percentage.toFixed(2) + '%')
});
//when write stream has finally written to file
dest.on('finish', callback)
} else {
console.log('EXITING......More than one/no file exist with same name, make sure you have unique file name.')
callback()
}
}
})
}
function downloadDriveFile () {
var fileName = 'testfile.doc'
callAfterDownload(fileName, function (err) {
if(err) throw err
//your logic to do anything with the file
})
}
downloadDriveFile();https://stackoverflow.com/questions/43340560
复制相似问题