0

I'm trying to write a program that dowloads OneNote pages to my pc, including files in the pages. I'm stuck on the downloading images from the pages. I make a GET request and get the binary data for the image just fine, when I save it and try to open it, I get a "it looks like we don't support this file format. The code I'm using is

var u16 =  btoa(unescape(encodeURIComponent(resp)));
var imgAsBlob = new Blob([u16], {type: 'application/octet-stream'});
var downloadLink = document.createElement("a");
downloadLink.download = "hello.png";
downloadLink.href = window.webkitURL.createObjectURL(imgAsBlob);
downloadLink.click();

resp is the responseText from the GET request with the binary data.

I've tried not using btoa and saving the resp directly on the blob. I've tried changing the blob type to image/png and I've tried escaping it using Uint16Array(resp.length) and equaling each byte to a byte from resp. I'm out of ideas and don't know what I'm doing wrong.

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
M. Araújo
  • 51
  • 3
  • It could be an option setting the href directly instead of making an ajax call `downloadLink.href = 'http://domain/imagepath.jpeg'`. – Roland Starke Jan 15 '20 at 13:40
  • My problem is that, like this, I can't add the header for the authorization code. I tried adding as a param, but it requires a space that is eliminated in HTML and non-breakable space doesnt work – M. Araújo Jan 15 '20 at 14:55
  • Ah okay, yes then you need ajax, but `var u16 = btoa(unescape(encodeURIComponent(resp))); var imgAsBlob = new Blob([u16], {type: 'application/octet-stream'});` is a little complicated and i guess there is the problem. You could take a look at how to use fetch. something like `fetch('http://domain/imagepath.jpeg').then(r => r.blob()).then(imgAsBlob => downloadLink.href = window.createObjectURL(imgAsBlob) )` (you would need to lookup how to use your auth headers there.) – Roland Starke Jan 15 '20 at 15:08
  • It works now! Thank you so much for the help! – M. Araújo Jan 15 '20 at 16:21

0 Answers0