React create and download file from buffer array

3.9k views Asked by At

I have an email attachment file in buffer format I want to display and download the file in ReactJS.

Sample JSON

{
   "type": "Buffer",
   "data": [137,80,78,71,13,10,26]
}

I want to download files in ReactJs or NodeJs.

1

There are 1 answers

2
Charlie On
const nodeJSBuffer = {
   "type": "Buffer",
   "data": [137,80,78,71,13,10,26]
}

const buffer = Buffer.from(nodeJSBuffer);
const blob = new Blob([buffer]);
                           
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
a.href = url;
a.download = "filename.pdf";
a.click();
window.URL.revokeObjectURL(url);