Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I download a Buffer that is returned from my server?

I have a Buffer that has the contains a PDF that I have not been able to successfully download. Every time I attempt to open the PDF, it fails to open. Here is the code I am using to download the file:

const content = new Blob(attach.content.data, { type: attach.contentType });

const encodedUri = window.URL.createObjectURL(content);
const link = document.createElement("a");

link.setAttribute("href", encodedUri);
link.setAttribute("download", attach.filename);

link.click();

Here is what the attach object looks like: enter image description here

Here is what the Blob looks like: enter image description here

Halp, plx!!!1

like image 606
Juliana Hill Avatar asked Oct 25 '25 02:10

Juliana Hill


1 Answers

The problem was that I was not converting the Node.js Buffer to a browser readable ArrayBuffer:

const data = Uint8Array.from(attach.content.data);
const content = new Blob([data.buffer], { type: attach.contentType });

const encodedUri = window.URL.createObjectURL(content);
const link = document.createElement("a");

link.setAttribute("href", encodedUri);
link.setAttribute("download", attach.filename);

link.click();
like image 74
Juliana Hill Avatar answered Oct 26 '25 23:10

Juliana Hill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!