Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

javascript - Insert a file to a particular folder using google-drive-api

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...