通过使用$ .get调用HTTPHandler来将PDF流化为object元素 [英] Streaming PDF via invocation of HTTPHandler using $.get into object element

查看:150
本文介绍了通过使用$ .get调用HTTPHandler来将PDF流化为object元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是通过jQuery的$ .get方法调用HTTPHandler,该方法将流回PDF并使用object元素将其显示在网页中.我之前将IFrame的src属性设置为处理程序调用的结果的方法有效,但是我想跨浏览器完成通知,因此已转为使用$ .get().示例代码:

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);
            });

如您所见,我正在尝试将'data'变量粘贴到一个object元素中.这是行不通的(没有错误,PDF不会显示),大概是因为返回的数据是二进制的,但是attr()方法需要一个字符串(我认为).

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).

因此,我的问题是:如何通过$ .get调用HTTPHandler并以某种方式将回调中的数据分配给对象的data属性?

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?

推荐答案

基于此问题:

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.

要使其正常工作,HTTP处理程序必须缓存结果,否则它将再次被调用.

For this to work the HTTP handler must be caching the results, otherwise it just gets invoked again.

这篇关于通过使用$ .get调用HTTPHandler来将PDF流化为object元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆