使用Javascript API进行Google云端硬盘文件上传/更新问题

时间:2015-01-23 10:39:59

标签: javascript file-upload google-drive-api

我正在尝试将Google驱动器可恢复文件上传/更新与我的应用程序集成。但是当我更新文件时,文件正在以编码格式更新,它没有采用实际内容。请找到以下详细信息

步骤1:启动可恢复会话

var request = gapi.client.request({
    "path": "/upload/drive/v2/files/"+fileId, 
    "method": "PUT",
    "params": {
        "uploadType": "resumable",       
    },
    "headers": {
        "X-Upload-Content-Type": "multipart/mixed",
    },
    "body": {                

    }
    });
    if (!callback) {
        callback = function(file) {
          console.log(file)
        };
    }   
    request.execute(callback);

步骤2:可恢复的会话发起请求

const boundary = '-------314159265358979323846';
  const delimiter = "\r\n--" + boundary + "\r\n";
  const close_delim = "\r\n--" + boundary + "--";

  //console.log(fileData.size);return false;

  var reader = new FileReader();
  reader.readAsBinaryString(fileData);
  reader.onload = function(e) {
    var contentType = fileData.type || 'application/octet-stream';
    //TO UPDATE A SLIDE YOU NEED TO UPLOAD SLIDE ONLY
    if(fileMetadata.mimeType == "application/vnd.google-apps.presentation" && contentType != "application/vnd.openxmlformats-officedocument.presentationml.presentation"){
        alert("Please upload a slide to update the file");
        return false;
    }
    // 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 accessToken = gapi.auth.getToken().access_token;
        var request = gapi.client.request({
        'path': sessionPath,
        'method': 'PUT',        
        'headers': {
          'Content-Type': 'multipart/mixed; boundary="' + boundary + '";Content-Length='+fileData.size,
          'Authorization ': 'Bearer'+ accessToken,
        },
        'body': multipartRequestBody});

    if (!callback) {
      callback = function(file) {
        console.log(file)
      };
    }    
    request.execute(callback);
  }

结果:更新了谷歌驱动器中的文件

---------314159265358979323846
Content-Type: application/json

{"mimeType":"application/vnd.google-apps.document","title":"sample doc 2201.docx","description":"test description.."}
---------314159265358979323846
Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
Content-Transfer-Encoding: base64

UEsDBBQACAgIAEMyN0YAAAAAAAAAAAAAAAALAAAAX3JlbHMvLnJlbHOtkk1LA0EMhu/9FUPu3WwriMjO9iJCbyL1B4SZ7O7Qzgczaa3/3kEKulCKoMe8efPwHNJtzv6gTpyLi0HDqmlBcTDRujBqeNs9Lx9g0y+6Vz6Q1EqZXCqq3oSiYRJJj4jFTOypNDFxqJshZk9SxzxiIrOnkXHdtveYfzKgnzHV1mrIW7sCtftI/Dc2ehayJIQmZl6mXK+zOC4VTnlk0WCjealx+Wo0lQx4XWj9e6E4DM7wUzRHz0GuefFZOFi2t5UopVtGd/9pNG98y7zHbNFe4ovNosPZG/SfUEsHCOjQASPZAAAAPQIAAFBLAwQUAAgICABDMjdGAAAAAAAAAAAAAAAAEQAAAHdvcmQvc2V0dGluZ3MueG1sRY5BTgMxEATvvMKae2KTQ0CreCNx4AMJDxjsye4Ke8byOFnC6zFw4NiqrlYfjp85mRtVXYQ9PG4dGOIgceHJw9v5dfMMRhtyxCRMHu6kcBwfDuug1FpvqekLrMPqYW6tDNZqmCmjbqUQd3aRmrH1WCe7So2lSiDVruZkd87tbcaFYeyTXyLZrEOhGohbv+Mc2B8QFy0J7y8YPqYqV46nGQv9IbrgNbUzvp+alG7fMHl42v2a9v/m+A1QSwcIJamct7kAAADrAAAAUEsDBBQACAgIAEMyN0YAAAAAAAAAAAAAAAAcAAAAd29yZC9fcmVscy9kb2N1bWVudC54bWwucmVsc62RTQrCMBCF954izN6mVRCRpm5EcCv1ADGdtsE2CckoensDiloo4sLl/H3vMS9fX/uOXdAH
你能告诉我我做错了吗?我是google drive apis的新手。

谢谢

0 个答案:

没有答案