Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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\r\n\r\n' +
    JSON.stringify(metadata) +
    delimiter +
    'Content-Type: ' + contentType + '\r\n' +
    'Content-Transfer-Encoding: base64\r\n' +
    '\r\n' +
    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);
like image 365
Mohammed H Avatar asked Oct 15 '12 05:10

Mohammed H


1 Answers

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

like image 192
Mohammed H Avatar answered Sep 18 '22 08:09

Mohammed H