ysytnik
ysytnik

Reputation: 15

Saving binary content of pdf file to javascript variable

Is it possible to save binary content of pdf into javascript variable? I have a webpage with url to saving pdf file and I need to save pdf file into javascript variable. I guess it should be binary data.

Upvotes: 0

Views: 1540

Answers (1)

joews
joews

Reputation: 30310

You can use XHR with response type blob:

var oReq = new XMLHttpRequest();
oReq.open("GET", "/myfile.pdf", true);
oReq.responseType = "blob";

oReq.onload = function(oEvent) {
  var blob = oReq.response;
  // `blob` contains the PDF content
};

oReq.send();

Source: Sending and Receiving Binary Data, MDN

Upvotes: 1

Related Questions