2012-02-03 89 views
0

我想下載多個pdf作爲附件在我的asp.net application.I創建了一些模板和使用pdfstamper填充值(itextsharp)。我能夠填寫值但不能下載。下載多個pdf

private void FillForm(string path, DataTable BridgeValues, DataTable Comments, DataTable Maintenance,string Newfilename) 
    { 
     try 
     { 
      string pdfTemplate = path; 
      string newFile = Newfilename; 
      string Pathser = ""; 
      if (!System.IO.Directory.Exists(Server.MapPath(@"~/PDF/"))) 
      { 
       System.IO.Directory.CreateDirectory(Server.MapPath(@"~/PDF/")); 
      } 

      if (Directory.Exists(Server.MapPath(@"~/PDF/"))) 
      { 
       Pathser = Server.MapPath(@"~/PDF/" + Newfilename); 
      } 
      System.IO.MemoryStream mStream = new System.IO.MemoryStream(); 
      // create a new PDF reader based on the PDF template document 
      PdfReader pdfReader = new PdfReader(pdfTemplate); 
      PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(Pathser, FileMode.Create)); 
      AcroFields pdfFormFields = pdfStamper.AcroFields; 
      DataColumn dc = null; 
      for (int i = 0; i < BridgeValues.Columns.Count - 1; i++) 
      { 
       dc = BridgeValues.Columns[i]; 
       pdfFormFields.SetField(dc.ColumnName.ToString(), BridgeValues.Rows[0][dc].ToString()); 
      } 
      pdfStamper.FormFlattening = true; 

      // close the pdf 
      pdfStamper.Close(); 
      ////Response.ContentType = "application/octet-stream"; 
      Response.ContentType = "application/pdf"; 
      ////Response.AddHeader("Content-Disposition", "attachment; filename=Report.pdf"); 
      Response.AddHeader("Content-Disposition", "attachment; filename=" + Newfilename + ""); 
      ////Response.BinaryWrite(mStream.ToArray()); 
      Response.TransmitFile(Server.MapPath(("~/PDF/"+ Newfilename))); 
      Response.Clear(); 
      Response.End(); 
     } 
     catch (System.Threading.ThreadAbortException lException) 
     { 

      // do nothing 

     } 
    } 

第一次我試着創建一個pdf,它工作,但後來當我試圖下載多個文件,它給了一個執行。 無法評估表達式,因爲代碼已經過優化或本機框架位於調用堆棧之上。

+1

爲什麼捕捉到ThreadAbortException?它會自動重新拋出,但你不應該捕捉它......至於例外 - *不會包含「無法評估表達式...」部分 - 這正是調試器顯示的內容,這不是例外的一部分。 – 2012-02-03 16:59:35

+0

這是我做的最後一次嘗試,出現了一些錯誤「無法評估表達式,因爲代碼已經優化或本機框架位於調用堆棧之上。」所以我嘗試了抓住部分。 – 2012-02-04 02:33:08

+0

不,這是*不是例外。它真的,真的不是。這正是調試器中顯示的內容。如果您查看郵件的例外情況,那不會是這樣。 – 2012-02-04 09:36:48

回答

1

我不認爲你可以通過提出一個請求並從操作中返回一個集合來下載多個文件。我建議您需要允許用戶下載多個文件,然後將它們壓縮到瀏覽器。

下面是壓縮多個文件的示例:http://devpinoy.org/blogs/keithrull/archive/2008/01/25/how-to-create-zip-files-in-c-with-sharpziplib-ziplib.aspx

+0

我正在循環瀏覽文件的數量並下載它們,但這是個不好的主意。如你所說我會以zip格式下載它們。我將許多文件保存在服務器上並將它們下載爲zip文件?任何示例? – 2012-02-04 02:30:16