2015-05-05 108 views
0

我對此很新,所以我正在通過試驗和錯誤進行學習。使用JavaScript下載多個文件

我有一個JavaScript,迄今爲止授權訪問用戶的Google Drive。它創建一個它發現的一些文件的數組,我可以訪問元數據來獲取每個文件的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); 


       } 
      }); 

如何讓腳本循環訪問每個文件數組元素並下載每個元素。我知道要使用'for循環',但觸發文件下載的實際方式是什麼,以便它們出現在我的瀏覽器下載文件夾中。

也,而這可能與使用gapi.client.request({/* etc */})

+0

請張貼代碼示例,這樣我們可以幫助 – Sam

回答

0

時,您可以創建一個iframe和Src設置爲將downloadURL值是什麼語法包括在HTTP請求中ALT =媒體參數。您可能需要設置超時...

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); 
     }; 
    } 
+0

感謝。我嘗試了window.location命令,它在只有1個文件的情況下工作,但我無法使其在數組中的每個文件中循環。這裏是我的嘗試: –

+0

request.execute(function(resp){ for(i = 0; i

+0

抱歉無法粘貼它沒有mangling所有格式 –