使用 google-drive-api 将文件插入特定文件夹

2022-01-10 00:00:00 google-drive-api javascript

我尝试如下.但该文件将进入根目录(我的驱动器).

var 元数据 = {标题":文件数据.文件名,'mimeType':内容类型,'parents':["0B6NmmF3ovpsbExuOEc1R2JzSFEp"]//这是我的文件夹 ID 之一.};var base64Data = btoa(reader.result);var multipartRequestBody =分隔符 +'内容类型:应用程序/json

' +JSON.stringify(元数据) +分隔符 +'内容类型:' + contentType + '
' +'内容传输编码:base64
' +'
' +base64数据 +close_delim;var request = gapi.client.request({'路径': '/upload/drive/v2/files','方法':'POST','params': {'uploadType': 'multipart'},标题":{'内容类型':'多部分/混合;边界='+边界+'"'},'body': multipartRequestBody});请求.执行(回调);

解决方案

我解决了这个问题.

错误在这一行:

'parents':["0B6NmmF3ovpsbExuOEc1R2JzSFEp"]

该行应该是:

'parents':[{"id":"0B6NmmF3ovpsbExuOEc1R2JzSFEp"}]

文档可在 https://developers.google.com/drive/web/folder 找到/p>

I tried as given below. But the file is going to root directory(My-Drive).

var metadata = {
  'title': fileData.fileName,
  'mimeType': contentType,
  'parents':["0B6NmmF3ovpsbExuOEc1R2JzSFEp"] // It is one of my folder's id.
};

var base64Data = btoa(reader.result);
var multipartRequestBody =
    delimiter +
    'Content-Type: application/json

' +
    JSON.stringify(metadata) +
    delimiter +
    'Content-Type: ' + contentType + '
' +
    'Content-Transfer-Encoding: base64
' +
    '
' +
    base64Data +
    close_delim;

var request = gapi.client.request({
    'path': '/upload/drive/v2/files',
    'method': 'POST',
    'params': {'uploadType': 'multipart'},
    'headers': {
      'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
    },
    'body': multipartRequestBody});

 request.execute(callback);

解决方案

I solved the issue.

The error was in this line:

'parents':["0B6NmmF3ovpsbExuOEc1R2JzSFEp"]

The line should be:

'parents':[{"id":"0B6NmmF3ovpsbExuOEc1R2JzSFEp"}]

Documentation can be found at https://developers.google.com/drive/web/folder

相关文章