我想使用的NodeJS表达对Google云端硬盘上传文件,从邮递员发送POST请求,我得到了错误“0 MIME部分无效的多部分请求。”,问题是请求主体,我认为,任何想法将不胜感激解决内这个问题或任何建议,谢谢。
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 });
}
});
我认为你的脚本几乎是正确的,你的脚本通过修改以下3点。
name
代替title
的财产。base64Data
可以通过new Buffer(data).toString('base64')
检索。json: true
的options
,发生Invalid multipart request with 0 mime parts.
的错误。请删除这一点。请修改如下。
title: file.name,
To:
name: file.name,
var base64Data = Buffer(data, 'base64');
To:
var base64Data = new Buffer(data).toString('base64');
请从json: true
删除options
。
如果这些修改不适合你的情况是有用的,我道歉。