如何仅更新谷歌驱动器文件的元数据

时间:2016-06-18 19:31:19

标签: javascript rest google-api

我想更新google驱动器文件的元数据。只是文件的描述。我提到了this,但它展示了如何更新文件的内容以及元数据。但是我只想更新文件的元数据。任何帮助将不胜感激。

function updateReadings(floor, reading) {
console.log(metadata);
var data = metadata.description;
var dataArray = data.split("\n");
var tenantNames = dataArray[floor - 1].split(":");
dataArray[floor - 1] = tenantNames[0] + ":" + reading;
data = dataArray[0] + "\n" + dataArray[1];
console.log(data);
    updateFile(fileId, metadata, null, null);
}

/**
 * Update an existing file's metadata and content.
 *
 * @param {String} fileId ID of the file to update.
 * @param {Object} fileMetadata existing Drive file's metadata.
 */
function updateFile(fileId, fileMetadata, fileData, callback) {
  const boundary = '-------314159265358979323846';
  const delimiter = "\r\n--" + boundary + "\r\n";
  const close_delim = "\r\n--" + boundary + "--";

  var reader = new FileReader();
  reader.readAsBinaryString(fileData);
  reader.onload = function(e) {
    var contentType = fileData.type || 'application/octet-stream';
    // Updating the metadata is optional and you can instead use the value from drive.files.get.
    var base64Data = btoa(reader.result);
    var multipartRequestBody =
        delimiter +
        'Content-Type: application/json\r\n\r\n' +
        JSON.stringify(fileMetadata) +
        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/' + fileId,
        'method': 'PUT',
        'params': {'uploadType': 'multipart', 'alt': 'json'},
        'headers': {
          'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
        },
        'body': multipartRequestBody});
    if (!callback) {
      callback = function(file) {
        console.log(file)
      };
    }
    request.execute(callback);
  }
}

0 个答案:

没有答案