从google picker挑选文件后立即下载文件 [英] Download file right after picked file from google picker

查看:217
本文介绍了从google picker挑选文件后立即下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是试图实施Google Drive Picker API,以便下载用户通过Google驱动器选择器提交的文件(在后台)。



我做了谷歌选择器,它运行良好,但后来,我只是无法下载文件。 (以单个文件开头)。

这是我的代码,在我的梦想中,我可以在获取选择器文件后立即下载文件。

  function createPicker(){
if(pickerApiLoaded&& oauthToken){
var picker = new google.picker.PickerBuilder )。
addView(google.picker.ViewId.DOCS)。
addView(google.picker.ViewId.PHOTOS)。
addView(google.picker.ViewId.FOLDERS)。
enableFeature(google.picker.Feature.MULTISELECT_ENABLED)。
setOAuthToken(oauthToken)。
setDeveloperKey(developerKey)。
setCallback(pickerCallback)。
build();
picker.setVisible(true);
}
}

//一个简单的回调实现。
函数pickerCallback(data){
var url ='nothing';
if(data [google.picker.Response.ACTION] == google.picker.Action.PICKED){
var fileId = data.docs [0] .id;
var fileUrl = data.docs [0] .url;
alert('用户选择:'+ fileId);
console.log(data.docs);
}
var message ='您选择了:'+ url;
document.getElementById('result')。innerHTML = message;
}

请注意,我想下载照片,并且我无法访问downloadUrl字段。
Google Drive Picker和Drive API在我的应用上处于开启状态。



除了我无法下载检索文件外,一切正常。

解决方案

您需要使用文件ID获取文件的下载网址。完成后,您可以使用AJAX调用该URL来获取文件数据。或者,您可以将文件字节作为blob在表单数据中发送到服务器端。

  var googleSelectedFiles = new Array(); 

if(data [google.picker.Response.ACTION] == google.picker.Action.PICKED){

var docs = data [google.picker.Response。文献];
docs.forEach(function(file){

var downloadUrl;

gapi.client.request({$ b $'path':'/ drive / v2 / files /'+ file.id,
'method':'GET',
callback:function(responsejs,responsetxt){

downloadUrl = responsejs.downloadUrl;

var gDoxBlob = null;
var xhr = new XMLHttpRequest();
xhr.open(GET,downloadUrl); //file.url

var accessToken = gapi.auth.getToken()。access_token;
xhr.setRequestHeader('Authorization','Bearer'+ accessToken);

xhr.responseType =blob ;
xhr.onload = function(){

gDoxBlob = xhr.response;
googleSelectedFiles.push({bytes:gDoxBlob,name:file.name});
}
xhr.send();

}
});

});

}


I am just trying to implement the Google Drive Picker API in order to download file (on background) submit by a user via Google drive picker.

I did the Google picker and it worked fine but then, I just couldn't download the file. (begin with single file first).

This is my code, in my dream I could download the file right after getting the picker's file.

function createPicker() {
            if (pickerApiLoaded && oauthToken) {
            var picker = new google.picker.PickerBuilder().
                    addView(google.picker.ViewId.DOCS).
                    addView(google.picker.ViewId.PHOTOS).
                    addView(google.picker.ViewId.FOLDERS).
                    enableFeature(google.picker.Feature.MULTISELECT_ENABLED).
                    setOAuthToken(oauthToken).
                    setDeveloperKey(developerKey).
                    setCallback(pickerCallback).
                    build();
            picker.setVisible(true);
        }
    }

    // A simple callback implementation.
    function pickerCallback(data) {
        var url = 'nothing';
        if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
            var fileId = data.docs[0].id;
            var fileUrl = data.docs[0].url;
            alert('The user selected: ' + fileId);
            console.log(data.docs);
        }
        var message = 'You picked: ' + url;
        document.getElementById('result').innerHTML = message;
    }

Note that I would like to download photo and that I have no access to a "downloadUrl" field. Google Drive Picker and Drive API are "On" on my app.

Everything works fine except that I can't download the retrieving file.

解决方案

You need to get the download URL of the file using its file id. Once done you can call that URL using AJAX to get the file data. Optionally, you can send the file bytes as blob in form data to server side.

var googleSelectedFiles = new Array();

if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {

    var docs = data[google.picker.Response.DOCUMENTS];
    docs.forEach(function (file) {

        var downloadUrl;

        gapi.client.request({
            'path': '/drive/v2/files/' + file.id,
            'method': 'GET',
            callback: function (responsejs, responsetxt) {

                downloadUrl = responsejs.downloadUrl;

                var gDoxBlob = null;
                var xhr = new XMLHttpRequest();
                xhr.open("GET", downloadUrl); //file.url

                var accessToken = gapi.auth.getToken().access_token;
                xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);

                xhr.responseType = "blob";
                xhr.onload = function () {

                    gDoxBlob = xhr.response;
                    googleSelectedFiles.push({ bytes: gDoxBlob, name: file.name });
                }
                xhr.send();

            }
        });

    });

}

这篇关于从google picker挑选文件后立即下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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