[英]How to fix Invalid multipart request with 0 mime parts error
I am trying to upload file using nodejs express to google drive, Sending post request from postman, i got the error "Invalid multipart request with 0 mime parts.", Problem is within the request body i think, Any idea would be grateful to solve this issue or any suggestions, thank you. 我正在尝试使用nodejs express将文件上传到Google驱动器,从邮递员发送发帖请求,我收到错误消息“无效的分段请求,其中mime部分为0。”问题在请求主体内,我认为,任何想法都将不胜感激这个问题或任何建议,谢谢。
let file = req.files.form_doc_20;
var contentType = file.type || 'application/octet-stream';
let parentId = 'root';
const boundary = '-------314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
var data = fs.readFileSync(file.path);
let metadata = {
title: file.name,
mimeType: contentType,
parents: [parentId]
};
var base64Data = Buffer(data, 'base64');
var multipartRequestBody =
delimiter +
"Content-Type: application/json\r\n\r\n" +
JSON.stringify(metadata) +
delimiter +
"Content-Type: " + contentType + "\r\n" +
"Content-Transfer-Encoding: base64\r\n" +
"\r\n" +
base64Data +
close_delim;
let options = {
url: 'https://www.googleapis.com/upload/drive/v3/files',
method: "POST",
headers: {
'Content-Type': "multipart/related; boundary=\"" + boundary + "\"",
'Authorization': "Bearer " + req.body.token_configuration.access_token,
},
body: multipartRequestBody,
qs: {
fields: "id, name, mimeType, modifiedTime, size",
uploadType: 'multipart'
},
json: true
};
helper.http_request(options, (err1, response) => {
if (err1) {
return res.json({ msg: 'Failed to upload the file.', error: response });
}
else {
return res.json({ result: response });
}
});
I think that your script is almost correct and your script works by modifying the following 3 points. 我认为您的脚本几乎是正确的,并且您的脚本通过修改以下3点来起作用。
name
instead of title
. name
属性代替title
。 base64Data
of the file can be retrieved by new Buffer(data).toString('base64')
. new Buffer(data).toString('base64')
检索文件的base64Data
。 json: true
of options
is used, the error of Invalid multipart request with 0 mime parts.
json: true
of options
,错误的Invalid multipart request with 0 mime parts.
的错误Invalid multipart request with 0 mime parts.
occurs. Please modify as follows. 请进行如下修改。
title: file.name,
To:
name: file.name,
var base64Data = Buffer(data, 'base64');
To:
var base64Data = new Buffer(data).toString('base64');
Please remove json: true
from options
. 请从
options
删除json: true
。
If these modifications were not useful for your situation, I apologize. 如果这些修改对您的情况没有帮助,我深表歉意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.