使用NodeJ将文件上传到Google云端硬盘时,如何获取进度状态? [英] How to get progress status while uploading files to Google Drive using NodeJs?
本文介绍了使用NodeJ将文件上传到Google云端硬盘时,如何获取进度状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用nodeJs将文件上传到Google云端硬盘时获取进度状态值.
Im trying to get progress status values while uploading files to google Drive using nodeJs.
controller.js
controller.js
exports.post = (req, res) => {
//file content is stored in req as a stream
// 1qP5tGUFibPNaOxPpMbCQNbVzrDdAgBD is the folder ID (in google drive)
googleDrive.makeFile("file.txt","1qP5tGUFibPNaOxPpMbCQNbVzrDdAgBD",req);
};
googleDrive.js
googleDrive.js
...
makeFile: function (fileName, root,req) {
var fileMetadata = {
'name': fileName,
'mimeType': 'text/plain',
'parents': [root]
};
var media = {
mimeType: 'text/plain',
body: req
};
var r = drive.files.create({
auth: jwToken,
resource: fileMetadata,
media: media,
fields: 'id'
}, function (err, file) {
if (err) {
// Handle error
console.error(err);
} else {
// r => undefined
console.log("Uploaded: " + r);
}
});
},
...
我遵循了这个链接,但始终未定义值
i followed this link but got always an undefined value
推荐答案
此修改如何?
- 它使用了
onUploadProgress
.
makeFile: function (fileName, root,req) {
var fileMetadata = {
'name': fileName,
'mimeType': 'text/plain',
'parents': [root]
};
var media = {
mimeType: 'text/plain',
body: req
};
var r = drive.files.create({
auth: jwToken,
resource: fileMetadata,
media: media,
fields: 'id'
}, {
onUploadProgress: function(e) {
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(e.bytesRead.toString());
},
}, function (err, file) {
if (err) {
// Handle error
console.error(err);
} else {
console.log("Uploaded: " + file.data.id);
}
});
},
注意:
- 如果要将进度显示为%",请使用文件大小.
- 已确认此脚本可在googleapis@33.0.0上使用.
- If you want to show the progression as "%", please use the file size.
- It was confirmed that this script worked at googleapis@33.0.0.
- axios
- test of google/google-api-nodejs-client
Note:
在我的环境中,我正在使用上面的脚本.但是,如果这在您的环境中不起作用,并且我误解了您的问题,对不起.
In my environment, I'm using the script like above. But if this didn't work in your environment and if I misunderstand your question, I'm sorry.
这篇关于使用NodeJ将文件上传到Google云端硬盘时,如何获取进度状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文