通过使用 $.get 调用 HTTPHandler 将 PDF 流式传输到对象元素中
我想做的是通过 jQuery 的 $.get 方法调用 HTTPHandler,该方法将流回 PDF 并使用对象元素将其显示在网页中。我之前将 IFrame 的 src 属性设置为处理程序调用结果的方法是有效的,但我想要跨浏览器完成通知,因此已转向使用 $.get()。示例代码:
function buttonClick() {
$.get("/PDFHandler.ashx", {},
function(data, textStatus, XMLHttpRequest) {
var pdfObjectString = "<object data='' type='application/pdf' width='600' height='600'></object>";
var pdfObject = $(pdfObjectString);
pdfObject.attr("data", data);
$("#container").append(pdfObject);
});
如您所见,我试图将“data”变量粘贴到对象元素中。这不起作用(没有错误,PDF只是不显示),大概是因为返回的数据是二进制的,但 attr() 方法需要一个字符串(我认为)。
因此,我的问题是:如何通过 $.get 调用 HTTPHandler 并以某种方式将回调中的数据分配给对象的 data 属性?
What I am trying to do is invoke an HTTPHandler via the $.get method of jQuery which will stream back a PDF and display it in a web page using an object element. My previous method of setting the src attribute of an IFrame to be the result of a handler invocation works, but I would like cross-browser completion notification, so have moved to using $.get(). Sample code:
function buttonClick() {
$.get("/PDFHandler.ashx", {},
function(data, textStatus, XMLHttpRequest) {
var pdfObjectString = "<object data='' type='application/pdf' width='600' height='600'></object>";
var pdfObject = $(pdfObjectString);
pdfObject.attr("data", data);
$("#container").append(pdfObject);
});
As you can see, I am attempting to stick the 'data' variable into an object element. This is not working (no error, PDF just doesn't display), presumably because the data that comes back is binary, yet the attr() method expects a string (I think).
My question is thus: how can I invoke an HTTPHandler via $.get and somehow assign the data from the callback to the data attribute of an object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基于这个问题:How to open a file using JavaScript? 我能够找到解决方案。本质上,您在成功回调函数中再次调用处理程序。我无法让它与
为此,HTTP 处理程序必须缓存结果,否则它只会被再次调用。
Based on this question: How to open a file using JavaScript? I was able to work out a solution. Essentially you call the handler again in the success callback function. I couldn't get it working with an
<object>
tag (still using an IFrame) but it is good enough for what I need.For this to work the HTTP handler must be caching the results, otherwise it just gets invoked again.
我花了好几个小时才完成这样的工作,这是我非常简单的解决方案,效果很好:
服务器返回pdf流并将响应内容类型设置为application/pdf。请参阅下面的代码(用 Delphi 编写)
变量
S:TMemoryStream;
//部分代码在服务器端生成pdf文件并转换为pdf文件流
//将流赋值给变量S并设置Response返回流
Response.ContentType := 'application/pdf';
Response.SendStream(S);
请注意,除非您想将其作为附件发送,否则不需要自定义标头,在这种情况下您可以发送 'Content-Disposition','attachment; filename="payslip.pdf"' 达到相同的结果。
I have struggled for hours to have something like this work, and this has been my very simple solution that works perfectly:
The server returns a pdf stream and sets the response content type to application/pdf. See below code (Written in Delphi)
Var
S: TMemoryStream;
//Some code generate the pdf file on the server side and convert it to a pdf file stream
//Assign the stream to the variable S and set the Response to return the stream
Response.ContentType := 'application/pdf';
Response.SendStream(S);
Note, no custom headers required for this unless you want to send it as attachment instead, in which case you can send 'Content-Disposition','attachment; filename="payslip.pdf"' to achieve the same result.