简体   繁体   English

如何修复Mime零件错误为0的无效零件请求

[英]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点来起作用。

Modification points: 修改要点:

  1. When you use Drive API v3, please use the property of name instead of title . 使用Drive API v3时,请使用name属性代替title
  2. base64Data of the file can be retrieved by new Buffer(data).toString('base64') . 可以通过new Buffer(data).toString('base64')检索文件的base64Data
  3. When 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 remove this. 请删除此。

Modified script: 修改后的脚本:

Please modify as follows. 请进行如下修改。

1: 1:

From: 从:
 title: file.name, 
To: 至:
 name: file.name, 

2: 2:

From: 从:
 var base64Data = Buffer(data, 'base64'); 
To: 至:
 var base64Data = new Buffer(data).toString('base64'); 

3: 3:

Please remove json: true from options . 请从options删除json: true

Note: 注意:

  • This modified script supposes that your environment can upload files using Drive API. 修改后的脚本假定您的环境可以使用Drive API上传文件。 If the error related to API occurs, please confirm whether Drive API is enabled. 如果发生与API相关的错误,请确认是否启用Drive API。 And also please confirm the scopes including in the access token. 另外,请确认访问令牌中包含的范围。
  • I could confirm that the modified script can upload a file to Google Drive. 我可以确认修改后的脚本可以将文件上传到Google云端硬盘。 So if other error occurs, it is considered that the other part which is not this script is the reason. 因此,如果发生其他错误,则认为不是此脚本的另一部分是原因。

References: 参考文献:

If these modifications were not useful for your situation, I apologize. 如果这些修改对您的情况没有帮助,我深表歉意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有Node.js googleapis包的Google Drive SDK引发错误:文件中包含0个mime部分的无效多部分请求 - Google Drive SDK with Node.js googleapis package throws Error: Invalid multipart request with 0 mime parts on files.create 将分段上传部分通过管道传输到新请求时出现“标头内容包含无效字符”错误 - "Header content contains invalid characters" error when piping multipart upload part into a new request 如何使用 jwt 修复错误“无效令牌”? - How to fix error "Invalid Token" with jwt? 多部分表单解析错误 - 多部分中的无效边界:无 - Multipart form parse error - Invalid boundary in multipart: None 如何修复502错误在请求后出现? - How to fix 502 error getting on post request? 如何将无效参数修复为 - How to fix Invalid Parameter To 如何修复传递给部分数据的部分作为属性添加的错误 - How to fix error with parts of the data passed through to partial being added as attributes 如何修复谷歌 API 'invalid_grant' 错误 - How to fix Google API 'invalid_grant' error “如何在超级账本编辑器中解决'错误:无效或丢失的标识符'” - “How to fix 'Error: Invalid or missing identifier' in hyperledger composer” 如何修复 Node 中的“ERROR _HTTP_INVALID_STATUS_CODE”? - How to fix 'ERROR _HTTP_INVALID_STATUS_CODE' in Node?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM