Google选择器选择了文件回调 [英] Google picker selected file callback
本文介绍了Google选择器选择了文件回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经集成并尝试使用Google Picker API Google选择器api 但是我可以将回调转换为文件吗?这样我就可以将文件上传到服务器了
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);
}
}
推荐答案
一旦有了 fileId
,就可以按照 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 根据您的情况.
you can modify xhr.responseType
to e.g. blob
or another responseType depending on your situation.
或者,您也可以执行获取请求转到"https://www.googleapis.com/drive/v3/files"以获取文件blob.
Alternatively you could also perform a Fetch request to 'https://www.googleapis.com/drive/v3/files' to obtain the file blob.
这篇关于Google选择器选择了文件回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文