2016-11-19 68 views
1

案例: 在Salesforce平臺上,我使用Google Drive以配置的Apex Google Drive API Framework存儲文件(此案例的圖像)。因此,Google Drive API處理authToken等。我可以在我的應用程序中上傳和瀏覽圖像。在我的情況下,我想選擇多個文件並將它們下載到一個zip文件中。到目前爲止,我正在嘗試使用JSZipFileSaver庫。使用下面相同的代碼我可以壓縮和下載多個文件存儲在其他地方與適當的響應標題,但不是從GDrive因爲CORS錯誤。如何使用Salesforce上的JSZip將多個文件從Google Drive下載爲.zip

https://xxx.salesforce.com/contenthub/download/XXXXXXXXXX%3Afile%XXXXXX_XXXXXXXXX. No'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://xxx.visual.force.com' is therefore not allowed access.如果我只是點擊這個鏈接,文件開始下載。

有什麼辦法可以配置GDrive來啓用響應頭:Access-Control-Allow-Origin: *Access-Control-Allow-Origin: https://*/mydomain.com不知何故,或者我只需要使用別的東西,也許服務器端的壓縮?現在,我使用的是Apex Google Drive API提供的下載鏈接(如下所示: https://xxx.salesforce.com/contenthub/download/XXXXXXXXXXX%3Afile%XXXXXXXX),當用作src="fileURL"或直接粘貼到瀏覽器時,它可以正常工作。 GDrive連接器添加'accesToken'等。

我的代碼:

//ajax request to get files using JSZipUtils 
let urlToPromise = (url) =>{ 
    return new Promise(function(resolve, reject) { 
     JSZipUtils.getBinaryContent(url, function (err, data) { 
      if(err) { 
      reject(err); 
      } else { 
      resolve(data); 
      } 
     }); 
    }); 
}; 

this.downloadAssets =() => { 
let zip = new JSZip(); 
//here 'selectedAssets' array of objects each of them has 'assetFiles' 
//with fileURL where I have url. Download and add them to 'zip' one by one 
for (var a of this.selectedAssets){ 
    for (let f of a.assetFiles){ 
     let url = f.fileURL;     
     let name = a.assetName + "." + f.fileType; 
     let filename = name.replace(/ /g, ""); 
     zip.file(filename, urlToPromise(url), {binary:true});          
    } 
} 
//generate zip and download using 'FileSaver.js'  
zip.generateAsync({type:"blob"}) 
    .then(function callback(blob) { 
    saveAs(blob, "test.zip"); 
    });  
}; 

我也試圖改變let url = f.fileURL由GDrive的連接器添加let url = f.fileURL + '?alt=media';&access_token=CURRENT_TOKEN

這個鏈接由GRDrive連接器處理,所以如果我只是在瀏覽器中輸入它下載圖像。但是,對於使用JS的多次下載,我得到了CORS錯誤。

回答

0

我以前發佈的代碼有效。忘了發佈解決方案。 只是不使用內容中心鏈接,我開始使用直接鏈接到谷歌驅動器和CORS問題已解決。仍然不確定CORS是否可以在Salesforce方面以某種方式解決。嘗試不同的設置,沒有運氣。 直接下載鏈接到GDrive在我的情況下工作正常。我唯一需要改變的是GDrive文件ID的前綴。

2

我覺得這個功能還不支持。如果您從Drive API檢查Download Files guide,則沒有提及一次下載多個文件。這是因爲你必須爲每個文件製作單獨的API請求。這在SO thread確認。

但是,選擇多個文件被轉換成單一的zip文件,並下載該谷歌驅動器API可能的單個zip文件。那麼如何將它們轉換爲單個Zip文件?請告訴我。

+0

是的,我沒有發現直接從GDrive下載多個文件的任何內容。我試圖一次下載帶有AJAX請求的單個文件'urlToPromise =(url)'。我想要做的是: 1.下載單個文件 2.在一個文件中的zip文件(在客戶端使用JavaScript) 3.下載'zip' 我需要類似於GDrive本身的東西,它可能在哪裏以zip形式下載多個文件。 –

2

根據我的說法,只需下載所有文件並將它們存儲在臨時目錄位置,然後將該目錄添加到zip文件並將該zip文件存儲到物理設備。

public Entity.Result<Entity.GoogleDrive> DownloadMultipleFile(string[] fileidList) 
    { 
     var result = new Entity.Result<Entity.GoogleDrive>(); 
     ZipFile zip = new ZipFile(); 

     try 
     { 
      var service = new DriveService(new BaseClientService.Initializer() 
      { 
       HttpClientInitializer = credential, 
       ApplicationName = "Download File", 
      }); 

      FilesResource.ListRequest listRequest = service.Files.List(); 
      //listRequest.PageSize = 10; 
      listRequest.Fields = "nextPageToken, files(id, name, mimeType, fullFileExtension)"; 

      IList<File> files = listRequest.Execute().Files; 

      if (files != null && files.Count > 0) 
      { 
       foreach (var fileid in fileidList) 
       { 
        foreach (var file in files) 
        { 
         if (file.Id == fileid) 
         {         
           result.Data = new Entity.GoogleDrive { FileId = fileid }; 
           FilesResource.GetRequest request = service.Files.Get(fileid); 
           request.ExecuteAsync(); 
           var stream = new System.IO.FileStream(HttpContext.Current.Server.MapPath(@"~\TempFiles") + "\\" + file.Name, System.IO.FileMode.Create, System.IO.FileAccess.Write); 
           request.MediaDownloader.ProgressChanged += (IDownloadProgress progress) => 
           { 
            switch (progress.Status) 
            { 
             case DownloadStatus.Downloading: 
              { 
               break; 
              } 
             case DownloadStatus.Completed: 
              { 
               break; 
              } 
             case DownloadStatus.Failed: 
              { 
               break; 
              } 
            } 
           }; 
           request.Download(stream); 
           stream.Close(); 
           break; 
          }         
         } 
        } 

      } 
      zip.AddDirectory(HttpContext.Current.Server.MapPath(@"~\TempFiles"), "GoogleDrive"); 
      string pathUser = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); 
      string pathDownload = System.IO.Path.Combine(pathUser, "Downloads"); 
      zip.Save(pathDownload + "\\GoogleDrive.zip"); 
      System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(HttpContext.Current.Server.MapPath(@"~\TempFiles")); 
      foreach (var file in di.GetFiles()) 
      { 
       file.Delete(); 
      } 

      result.IsSucceed = true; 
      result.Message = "File downloaded suceessfully"; 
     } 
     catch (Exception ex) 
     { 
      result.IsSucceed = false; 
      result.Message = ex.ToString(); 
     } 
     return result; 
    } 
相關問題