2013-02-05 36 views
1

我試圖按照此文章在這裏流的PDF到用戶的瀏覽器:http://www.4guysfromrolla.com/articles/030911-1.aspx使用iTextSharp的

我在Services.asmx這個方法:

[WebMethod] 
public void CreatePdf() 
{ 
    // Create a Document object 
    var document = new Document(PageSize.A4, 50, 50, 25, 25); 

    // Create a new PdfWriter object, specifying the output stream 
    var output = new MemoryStream(); 
    var writer = PdfWriter.GetInstance(document, output); 

    // Open the Document for writing 
    document.Open(); 

    // Create a new Paragraph object with the text, "Hello, World!" 
    var welcomeParagraph = new Paragraph("Hello, World!"); 

    // Add the Paragraph object to the document 
    document.Add(welcomeParagraph); 

    // Close the Document - this saves the document contents to the output stream 
    document.Close(); 

    HttpContext.Current.Response.ContentType = "application/pdf"; 
    HttpContext.Current.Response.AddHeader("Content-Disposition", 
     "attachment;filename=file.pdf"); 
    HttpContext.Current.Response.BinaryWrite(output.ToArray());   
} 

這jQuery代碼我的網頁上:

$('a.download').click(function() { 

    $.ajax({ 
     type: "POST", 
     url: "/Services.asmx/CreatePdf", 
     data: '{}', 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (result) { 
      alert(result.d); 
     } 
    }); 
}); 

這應該是創建一個pdf並將其傳輸到用戶的瀏覽器。

當我點擊類download的鏈接時,我的web方法被擊中並且代碼運行。它不會將PDF傳輸到瀏覽器。

如果我看螢火蟲,它發佈到我的方法與狀態200,我得到這個答覆:

%PDF-1.4 % 2 0 OBJ <>流 X + R 25P04WI2P51BҸ4>>> /目錄2 0 R /父3 0 R >> endobj 1 0 OBJ <> endobj 3 0 obj <> endobj 5 0 OBJ <> endobj 6 0 OBJ <> endobj 外部參照 0000000000 65535˚F 0000000304 00000Ñ 0000000015 00000Ñ 0000000392 00000Ñ 0000000147 00000Ñ 0000000443 00000Ñ 0000000488 00000ñ 拖車 < < 21ba8d519bb56a2d0ec514bcb9c47169>] >> %的iText-5.3.5 startxref %% EOF { 「d」:空}

我在這裏幹什麼什麼了嗎?

+7

您無法通過ajax下載文件。而不是使用ajax,使用一個正常的形式和帖子。 –

回答

4

Marc B是正確的。您需要讓您的服務器端代碼以pdf輸出流進行響應。

因此,將您的下載鏈接指向一個新文件,比如說PDFDownload.aspx,並將CreatePdf函數中的代碼放入PDFDownload.aspx.cs的PageLoad中。

1

它看起來像你使用xmlhttprequest時不能接收二進制數據。 (這是jquery所做的)。 當你做一個form post一個標準a href link

它應該工作。因爲然後響應類型由瀏覽器處理...

確保您在服務器上設置匹配的標頭,就像您一樣...

contentDisposition = "attachment=\"" name ""; 
contentType = "application/pdf"; 

希望這有助於

+0

這是一個很好的答案 - 使用正常的表單發佈,沒有ajax - doc會返回不刷新頁面 –

1

那麼我這樣做,這樣:在頁面

方法aspx.cs

[WebMethod()] 
    public static string CreatePdf() 
    { 

     System.IO.MemoryStream ms = new System.IO.MemoryStream(); 
     iTextSharp.text.Document doc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 30f, 30f, 30f, 30f); 
     iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, ms); 

     doc.Open(); 
     doc.Add(new iTextSharp.text.Chunk("hello world")); 
     doc.Close(); 
     // convert ms to byte and Base64 
     return System.Convert.ToBase64String(ms.ToArray()); 


    } 

功能的jQuery

$("#createPdf").click(function() { 
     //call ajax 
     $.ajax({ 
      url: "main.aspx/CreatePdf", 
      data: '{}', 
      type: "POST", 
      dataType: "json", 
      contentType: "application/json; charset=utf-8", 
      success: function (result) { 
       //pdf 
       var downloadpdf = $('<a id="downloadpdf" download="file.pdf" href="data:application/pdf;base64,' + result.d + '" >'); 
       $('body').append(downloadpdf); 
       document.getElementById("downloadpdf").click(); 
       $("#downloadpdf").remove(); 
      }, 
      error: function (req, status, error) { 
       alert(error); 
      } 
     }); 
    });         

希望它幫助別人!