如何使用谷歌驱动器API上传FILE_URI:插入文件驱动器、如何使用、上传、文件

2023-09-05 09:05:03 作者:Bleeding 滥好人

在Android上,我试图上传从科尔多瓦/ PhoneGap的Getpicture中(),使用谷歌驱动器API输出:插入文件。有没有一种方法使用FILE_URI代替DATA_URL的(Base64)?要做到这一点

我试过Camera.DestinationType.DATA_URL第一,但它并没有返回Base64的数据好像是应该的,它只是返回的同样的事情FILE_URI。所以现在我试图找出如何通过FILE_URI到谷歌驱动器插入文件(这需要的Base64)。有没有一种方法来FILE_URI转换为Base64?

科尔多瓦code:

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

功能的onSuccess(imageURI){
    VAR图像=的document.getElementById('MYIMAGE');
    image.src = imageURI;

    //需要做这样的事情:
    VAR的FileData = ConvertToBase64(imageURI);
    的insertFile(的FileData);
}
 

谷歌驱动器code:

  / **
 *将新的文件。
 *
 * @参数{}文件的文件的FileData对象从读取数据。
 当请求完成*参数{}功能回调函数调用。
 * /
功能的insertFile(的FileData,回调){
  常量边界='------- 314159265358979323846;
  常量分隔符=\ r \ N--+边界+\ r \ N的;
  常量close_delim =\ r \ N--+边界+ - ;

  VAR读卡器=新的FileReader();
  reader.readAsBinaryString(的FileData);
  reader.onload =功能(E){
    VAR的contentType = fileData.type || 应用程序/八位字节流;
    VAR元= {
      标题:fileData.fileName,
      MIMETYPE:的contentType
    };

    VAR base64Data = BTOA(reader.result);
    VAR multipartRequestBody =
        分隔符+
        内容类型:应用程序/ JSON \ r \ñ\ r \ N'+
        JSON.stringify(元数据)+
        分隔符+
        内容类型:+的contentType +\ r \ N'+
        内容传输编码:BASE64 \ r \ N'+
        \ r \ N'+
        base64Data +
        close_delim;

    VAR请求= gapi.client.request({
        路径:/上传/驱动器/ V2 /文件',
        方法:POST,
        PARAMS:{uploadType:多部分},
        头:{
          内容类型:/混合;边界=+边界+
        },
        身体:multipartRequestBody});
    如果(!回调){
      回调=功能(文件){
        的console.log(文件)
      };
    }
    request.execute(回调);
  }
}
 

解决方案

是:您可以....

指定目标类型 FILE_URI 本身和的ImageData你会得到的图像文件的URI的地方在一个图像标记,然后将其放在 HTML5的画布和帆布里面已经叫了一个方法 toDataURL 在这里您将能够获得相应的图像的Base64编码。

 函数的onSuccess(为imageData)
     {

                变量$ IMG = $('< IMG />');
                $ img.attr('src'中,为imageData);
                $ img.css({位置:绝对,离开了:0px​​,上面:-999999em',了maxWidth:'无',宽度:'自动',高度:'自动'});
                $ img.bind('负荷',函数()
                {
                    VAR帆布= document.createElement方法(帆布);
                    canvas.width = $ img.width();
                    canvas.height = $ img.height();
                    变种CTX = canvas.getContext('二维');
                    ctx.drawImage($ IMG [0],0,0);
                    VAR dataUri = canvas.toDataURL('图像/ PNG);

                });
                $ img.bind('错误',函数()
                {
                    的console.log('不可能转换照片数据URI);
                });

    }
 
使用HDFS JAVA API上传文件

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)?

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?

Cordova code:

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 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 = "\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);
  }
}

解决方案

Yes you can....

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

    }