我正在使用适用于Javascript的Dropbox V2 API,我希望获得某种状态更新,以了解下载或上传的距离-传输的数据量占文件总大小的百分比正在上传。
遗憾的是,Dropbox API v2 JavaScript SDK无法提供跟踪进度信息的方法,但我们会将其视为功能请求。
Not sure whether you want this problem to be solved on browser / node.js side, but here is an example how to do it in node.js :
Output:
1% downloaded
5% downloaded
7% downloaded
40% downloaded
54% downloaded
90% downloaded
file downloaded!
100% downloaded
Source:
/**
* To run example you have to create 'credentials.json' file
* in current location.
*
* File should contain JSON object, with 'TOKEN' property.
*/
const dropboxV2Api = require('dropbox-v2-api');
const progress = require('progress-stream');
const fs = require('fs');
const path = require('path');
const credentials = JSON.parse(fs.readFileSync(path.join(__dirname, 'credentials.json')));
//set token authentication:
const dropbox = dropboxV2Api.authenticate({
token: credentials.TOKEN
});
const FILE_PATH = '/dropbox/file.txt';
getSizeByFilePath(FILE_PATH, (err, fileSize) => {
if(err) return console.log(err);
getFileStream(FILE_PATH)
.pipe(getProgresStreamByFileSize(fileSize))
.pipe(fs.createWriteStream('.' + FILE_PATH));
});
function getProgresStreamByFileSize(fileSize) {
const progressStream = progress({
length: fileSize,
time: 100 // print progress each 100 ms
});
progressStream.on('progress', function(progress) {
const percentage = parseInt(progress.percentage, 10)+'%';
console.log(`${percentage} downloaded`);
});
return progressStream;
}
function getFileStream(filePath) {
return dropbox({
resource: 'files/download',
parameters: {
path: filePath
}
}, (err, result) => {
if(err) return console.log(err);
console.log('file downloaded!')
});
}
function getSizeByFilePath(filePath, cb) {
dropbox({
resource: 'files/alpha/get_metadata',
parameters: {
'path': filePath,
'include_media_info': false,
'include_deleted': false,
'include_has_explicit_shared_members': false
}
}, (err, result) => {
if(err) return cb(err);
cb(null, result.size);
});
}
Following packages: progress-stream and dropbox-v2-api used in this example.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.