views:

65

answers:

1

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?

A: 

Based on this question: http://stackoverflow.com/questions/761682/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.