如何使用Google Drive API上传FILE_URI:插入文件

wis*_*cky 5 mobile android cordova google-drive-api

在Android上,我尝试使用Google Drive API:插入文件从Cordova / Phonegap getPicture()上传输出。有没有办法使用FILE_URI而不是DATA_URL(base64)来做到这一点?

我首先尝试了Camera.DestinationType.DATA_URL,但它没有返回应有的Base64数据,只是返回了与FILE_URI相同的内容。所以现在我想弄清楚如何将FILE_URI传递给Google Drive插入文件(需要Base64)。有没有一种方法可以将FILE_URI转换为Base64?

科尔多瓦代码:

navigator.camera.getPicture(onSuccess, onFail,
    { quality: 50, destinationType: Camera.DestinationType.FILE_URI });

function onSuccess(imageURI) {
    var image = document.getElementById('myImage');
    image.src = imageURI;

    // need to do something like this:
    var fileData = ConvertToBase64(imageURI);
    insertFile(fileData);
}
Run Code Online (Sandbox Code Playgroud)

Google云端硬盘代码:

/**
 * Insert new file.
 *
 * @param {File} fileData File object to read data from.
 * @param {Function} callback Function to call when the request is complete.
 */
function insertFile(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';
    var metadata = {
      'title': fileData.fileName,
      'mimeType': contentType
    };

    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});
    if (!callback) {
      callback = function(file) {
        console.log(file)
      };
    }
    request.execute(callback);
  }
}
Run Code Online (Sandbox Code Playgroud)

Aru*_*til 2

是的你可以....

指定Destination TypeFILE_URI本身,在 imagedata 中,您将获取图像文件 uri 将其放入图像标签中,然后将其放入内部HTML5 canvas,canvas 有一种名为toDataURL的方法,您可以在其中获取相应图像的 base64。

function onSuccess(imageData)
     {

                var $img = $('<img/>');
                $img.attr('src', imageData);
                $img.css({position: 'absolute', left: '0px', top: '-999999em', maxWidth: 'none', width: 'auto', height: 'auto'});
                $img.bind('load', function() 
                {
                    var canvas = document.createElement("canvas");
                    canvas.width = $img.width();
                    canvas.height = $img.height();
                    var ctx = canvas.getContext('2d');
                    ctx.drawImage($img[0], 0, 0);
                    var dataUri = canvas.toDataURL('image/png');

                });
                $img.bind('error', function() 
                {
                    console.log('Couldnt convert photo to data URI');
                });

    }
Run Code Online (Sandbox Code Playgroud)