流媒体使用iTextSharp的pdf文件到用户的浏览器 [英] Streaming a pdf to the user's browser using itextsharp
问题描述
我试图按照此文章在这里: HTTP://www.4guysfromrolla。 COM /用品/ 030911-1.aspx
I'm trying to follow this article here: http://www.4guysfromrolla.com/articles/030911-1.aspx
我在Services.asmx此方法:
I have this method in 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 code:
And this jQuery code on my page:
$('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文件,并传输到用户的浏览器。
This is supposed to create a pdf and stream it to the user's browser.
当我点击与类的链接下载
,我的Web方法被击中和code运行。它只是不流了PDF浏览器。
When I click on a link with the class download
, my web method gets hit and the code runs. It just doesn't stream the pdf to the browser.
如果我在看萤火虫,它发布到我的方法与状态200,而我得到这个答复:
If I look in Firebug, it posts to my method with status 200, and I get this Response:
%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
外部参照
0 7
0000000000 65535˚F
0000000304 00000ñ
0000000015 00000ñ
0000000392 00000ñ
0000000147 00000ñ
0000000443 00000ñ
0000000488 00000ñ
拖车
<< 21ba8d519bb56a2d0ec514bcb9c47169>] >>
%iText的-5.3.5
startxref
646
%% EOF
{D:空}
%PDF-1.4 %���� 2 0 obj <>stream x�+�r �25P�04WI�2P�5��1���BҸ4>>>/Contents 2 0 R/Parent 3 0 R>> endobj 1 0 obj <> endobj 3 0 obj <> endobj 5 0 obj <> endobj 6 0 obj <> endobj xref 0 7 0000000000 65535 f 0000000304 00000 n 0000000015 00000 n 0000000392 00000 n 0000000147 00000 n 0000000443 00000 n 0000000488 00000 n trailer <<21ba8d519bb56a2d0ec514bcb9c47169>]>> %iText-5.3.5 startxref 646 %%EOF {"d":null}
难道我做错了吗?
推荐答案
马克·B是正确的。你需要有服务器端code与PDF输出流响应。
Marc B is correct. You need to have your server-side code respond with the pdf output stream.
所以,将您的下载链接到一个新的文件,说PDFDownload.aspx,并将code从CreatePdf功能PDFDownload.aspx.cs的pageLoad的。
So, point your download link to a new file, say PDFDownload.aspx, and place the code from your CreatePdf function in the PageLoad of PDFDownload.aspx.cs.
这篇关于流媒体使用iTextSharp的pdf文件到用户的浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!