Google picker selected file callback

时光怂恿深爱的人放手 提交于 2021-01-28 13:33:33

问题


i have integrated and try to use the google picker api google picker api but can i turn the callback into file ? so i can upload the file to my server

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


回答1:


Once you have the fileId, you can download the file as explained in the documentation for Download files with the Drive API

Unfortunately, there is no sample for Browser Javascript, but you use e.g. XML HttpRequests.

Sample:

//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();

Note:

you can modify xhr.responseType to e.g. blob or another responseType depending on your situation.

Alternatively you could also perform a Fetch request to 'https://www.googleapis.com/drive/v3/files' to obtain the file blob.



来源:https://stackoverflow.com/questions/64043752/google-picker-selected-file-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!