如何使用 Google Drive API 上传 FILE_URI:插入文件 [英] How to upload FILE_URI using Google Drive API: Insert File

查看:16
本文介绍了如何使用 Google Drive API 上传 FILE_URI:插入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

On Android, I'm trying to upload the output from Cordova/Phonegap getPicture() using Google Drive API: Insert File. Is there a way to do this using the FILE_URI instead of DATA_URL (base64)?

我首先尝试了 Camera.DestinationType.DATA_URL,但它没有像预期的那样返回 Base64 数据,它只是返回与 FILE_URI 相同的内容.所以现在我想弄清楚如何将 FILE_URI 传递给 Google Drive Insert File(需要 Base64).有没有办法将 FILE_URI 转换为 Base64?

I tried Camera.DestinationType.DATA_URL first, but it didn't return Base64 data like it was supposed to, it just returned the same thing as FILE_URI. So now I'm trying to figure out how to pass FILE_URI to Google Drive Insert File (which takes Base64). Is there a way to convert FILE_URI to 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);
}

Google 云端硬盘代码:

Google Drive code:

/**
 * 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 = "
--" + boundary + "
";
  const close_delim = "
--" + 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

' +
        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});
    if (!callback) {
      callback = function(file) {
        console.log(file)
      };
    }
    request.execute(callback);
  }
}

推荐答案

是的你可以....

Destination Type 指定为 FILE_URI 本身,在 imagedata 中,您将获得图像文件 uri 将其放置在图像标记中,然后将其放置在 HTML5 画布中 和 canvas 有一个名为 toDataURL 的方法,您可以在其中获取相应图像的 base64.

Specify Destination Type as FILE_URI itself and in imagedata you will be getting the images file uri place it in a image tag and then place it inside HTML5 canvas and canvas has one method called toDataURL where you will be able to get the base64 of the corresponding image.

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');
                });

    }

这篇关于如何使用 Google Drive API 上传 FILE_URI:插入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆