user946924
user946924

Reputation: 75

Javascript on Firefox: Accessing binary data from html object possible?

On an html page I have an <object> that hosts a pdf. I would need to access the binary data of the pdf via Javascript, but I cannot figure out how to accomplish that. I get access to the object element itself but cannot think of a method for getting the data in it.

Is it possible at all?

Upvotes: 0

Views: 647

Answers (1)

copy
copy

Reputation: 3372

You can not get the binary from an object tag, but you can make an AJAX request to the server and get it as ArrayBuffer by using the new responseType attribute:

var http = new XMLHttpRequest();

http.open("get", "somefile.pdf", true);
http.responseType = "arraybuffer";

http.onload = function(e)
{
    if(http.response)
    {
        // http.response contains the file
    }
};

http.send(null);

Note that this method only works in newer browsers and is obviously restricted by the Same-Origin-Policy.

Upvotes: 2

Related Questions