2016-03-01 72 views
1

我有一個安裝程序,我需要從數據庫中下載多個文件(至少2個),其中每個文件通過ASP存儲爲二進制數組。我發現了一個微軟的例子,幾乎可以做到這一點,但我似乎無法下載多個文件。它總是下載1個文件(即使有2個文件),然後將其稱爲「FullActivity」(這是我創建的類的名稱)。看起來像這樣:通過ASP下載多個文件

protected void download_btn_Click(object sender, EventArgs e) 
{ 
    Int32 id = Int32.Parse(Request.QueryString.Get(0)); 

    List<ActivityFile> files = Facade.GetActivityFiles(id); 
    StringBuilder msg = new StringBuilder(); 
    if (files.Count == 0) 
    { 
     msg.Append("No files were found for this activity."); 
     String jsExec = Util.ModalAlert(msg.ToString(), "#info_modal", ".modal-body"); 
     ScriptManager.RegisterStartupScript(Page, GetType(), "ModalAlertShow", jsExec, false); 
    } 
    else 
    { 
     foreach (ActivityFile file in files) 
     { 
      //TODO: Perhaps find a dynamic way of doing this 
      String folder = "\\\\jetfile01\\Users\\" + Util.GetUserAccount(this); 
      switch (file.FileType) 
      { 
       case "pdf": 
        Response.ContentType = "Application/pdf"; 
        break; 
       case "docx": 
        Response.ContentType = "Application/msword"; 
        break; 
       case "doc": 
        Response.ContentType = "Application/msword"; 
        break; 
       case "xlsx": 
        Response.ContentType = "Application/x-msexcel"; 
        break; 
       case "xls": 
        Response.ContentType = "Application/x-msexcel"; 
        break; 
       default: 
        Response.ContentType = "text/plain"; 
        break; 
      } 
      String filepath = MapPath(file.Name + "." + file.FileType); 
      Response.BinaryWrite(file.FileBuffer); 
      Response.Flush(); 
     } 
     Response.End(); 
    } 
} 

我不知道我失蹤了什麼,我不知道如何實際命名文件。我的ActivityFile類包含文件的全名和擴展名,但我不確定如何在這種情況下使用這些信息。

任何幫助?

+2

一個響應對象只能存儲一個文件的字節。使用.ZIP庫(如https://dotnetzip.codeplex.com/)將所有文件壓縮成一個.zip文件。 – adaam

+0

n.b.我以前的陳述可能不一定是真實的,它可能確實可以實現,但爲了簡單起見,只需創建一個zip文件! – adaam

回答

1

每個文件下載都是一個響應。

每個請求只能有一個響應(當然從推送通知開!)

相反,呈現其或者包含鏈接,或使單獨的請求下載各個文件的頁面。

或者,下載一個包含多個文件的zip壓縮文件。

[編輯]

我只是想過這個多一點,我沒有再相信這是真的!

這是我可能會這樣做,但。

[/編輯]

IFRAME溶液HTML

<iframe id='frame1' name='frame1' /> 
<iframe id='frame2' name='frame2' /> 

的腳本:

document.getElementById('frame1').src = 'downloadfile1.csv'; 
document.getElementById('frame2').src = 'downloadfile2.csv'; 
+0

好的,謝謝,我會研究一下。 – OmniOwl

+0

嗯,我嘗試了您建議的打開多個頁面的方式,但它只打開第一頁,即使我啓動兩個線程打開多個頁面:http://pastebin.com/umXs8jK1然後下載該文件。 – OmniOwl

+0

我的意思是讓你從一個頁面上做出幾個下載請求。嘗試將呼叫分開500毫秒,以便您的請求不會發生衝突 - 可能會更少。 – CompanyDroneFromSector7G