使用JavaScript

时间:2015-05-05 12:43:20

标签: javascript google-api

我对此非常陌生,所以通过反复试验来学习。

我有一个javascript,到目前为止授权访问用户Google云端硬盘。它创建了一个它找到的一些文件的数组,我可以访问元数据来获取每个文件的DownloadURL。

function downloadFile() {
            gapi.client.load('drive', 'v2', makeRequest);
        }


        function makeRequest() {
            var request = gapi.client.request({
                    'path': '/drive/v2/files',
                    'method': 'GET',
                    'params': {'q': 'title contains "This"'} // retrieve all files with a filename that begins with the word "This"
                });

            request.execute(function(resp) {

                for (i = 0; i < resp.items.length; i++) {
                    var dlUrl = resp.items[i].downloadUrl; //this is the url extracted from the request
                    var finalDlUrl = dlUrl.split("&gd=true");//the url needs to be trimmed so the last 8 characters are ignored in order to work
                   window.location.assign(finalDlUrl);


                }
            });

如何让脚本循环遍历每个文件数组元素并下载每个元素。我知道要使用&#39; for循环&#39;但触发文件下载的实际方式是什么,以便它们出现在我的浏览器下载文件夹中。

另外,它可能是相关的,使用gapi.client.request({/* etc */})

时,在http请求中包含alt = media参数的语法是什么

1 个答案:

答案 0 :(得分:0)

您可以创建iframe并将src设置为DownloadURL值。您可能需要设置超时...

    function downloadFile() {
        gapi.client.load('drive', 'v2', makeRequest);
    }


    function makeRequest() {
        var request = gapi.client.request({
                'path': '/drive/v2/files',
                'method': 'GET',
                'params': {'q': 'title contains "This"'} // retrieve all files with a filename that begins with the word "This"
        });

        request.execute(function(resp) {
            for (i = 0; i < resp.items.length; i++) {
                var dlUrl = resp.items[i].downloadUrl; //this is the url extracted from the request
                var finalDlUrl = dlUrl.split("&gd=true");//the url needs to be trimmed so the last 8 characters are ignored in order to work
               downloadUrl(finalDlUrl);
            }
        });
    }

    function downloadUrl(url) {
        var iframe = document.createElement("iframe");
        iframe.src = url;
        iframe.style.display = "none";
        document.body.appendChild(iframe);
    }

以下是一些可能按顺序下载文件的代码:

    function downloadFile() {
        gapi.client.load('drive', 'v2', makeRequest);
    }

    function makeRequest() {
        var request = gapi.client.request({
                'path': '/drive/v2/files',
                'method': 'GET',
                'params': {'q': 'title contains "This"'}
            });

        request.execute(function(resp) {
            downloadFiles(0, resp.items);
        });
    }

    function downloadFiles(current, list)
    {
        var next = current + 1;
        if(next == list.length) {
            return;
        }

        var dlUrl = resp.items[next].downloadUrl;
        var finalDlUrl = dlUrl.split("&gd=true");

        var iframe = document.createElement("iframe");
        iframe.src = finalDlUrl;
        iframe.style.display = "none";
        document.body.appendChild(iframe);

        iframe.onload = function() {
            downloadFiles(next, list);
        };
    }