Skip to content

Instantly share code, notes, and snippets.

@dpawluk
Created December 6, 2016 20:41
Show Gist options
  • Select an option

  • Save dpawluk/682220ca61b0913cf64adfeae9a457fc to your computer and use it in GitHub Desktop.

Select an option

Save dpawluk/682220ca61b0913cf64adfeae9a457fc to your computer and use it in GitHub Desktop.
Get PDF from URL, create Object URL, then download/open in a browser window
(function() {
return {
events: {
'app.activated':'init'
},
init: function() {
var self = this;
this.switchTo('main');
var req = new XMLHttpRequest();
var pdf_url = this.assetURL("test2.pdf");
req.open("GET", pdf_url, true);
req.responseType = "blob";
console.log(req);
req.onload = function (event) {
console.log("loaded");
var blob = req.response;
var link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = "name_of_file.pdf";
link.text = "Get PDF";
link.target = "_blank";
self.$("#appwrap").append(link);
self.$("#appwrap a").click();
};
req.send();
}
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment