【问题标题】:Google picker selected file callback谷歌选择器选择文件回调
【发布时间】:2020-09-24 09:42:34
【问题描述】:

我已集成并尝试使用 google picker api google picker api 但我可以把回调变成文件吗? 所以我可以将文件上传到我的服务器

function pickerCallback(data) { 
  if (data.action == google.picker.Action.PICKED) {
    var fileId = data.docs[0].id; alert('The user selected: ' + fileId); 
  } 
} 

【问题讨论】:

    标签: javascript google-api google-drive-api google-oauth google-picker


    【解决方案1】:

    获得fileId 后,您可以按照Download files with the Drive API 文档中的说明下载该文件

    不幸的是,没有浏览器 Javascript 的示例,但您使用例如XML HttpRequests.

    示例:

    //provided you already have the following line from previous steps:
    oauthToken = authResult.access_token;
    
    var doc = data[google.picker.Response.DOCUMENTS][0];
    var mimeType = doc[google.picker.Document.MIME_TYPE];
    
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "https://www.googleapis.com/drive/v3/files/"+fileId+'?alt=media', true);
    xhr.setRequestHeader('Authorization','Bearer '+oauthToken);
    xhr.responseType = 'arraybuffer'
    xhr.onload = function(){
        var base64 = 'data:' + mimeType + ';base64,' + base64ArrayBuffer(xhr.response);
    //now you have the file content as binary data (ArrayBuffer) - proceed as desired
    }
    xhr.send();
    

    注意:

    您可以将xhr.responseType 修改为例如blob 或另一个 responseType 取决于您的情况。

    或者,您也可以对“https://www.googleapis.com/drive/v3/files”执行Fetch request 以获取文件 blob。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-04
      • 1970-01-01
      相关资源
      最近更新 更多