Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 13 - Download file from ByteArray data

I get a byte[] file from my server with the content of a file. I know the name and the content-type. So far I have tried the following for downloading the file:

const a = document.createElement('a');
document.body.appendChild(a);
a.style.display = 'none';

const file = new Blob([content], {type: 'text/plain'});
const url = window.URL.createObjectURL(file);
a.href = url;
a.download = "test.txt";
a.click();
window.URL.revokeObjectURL(url);

But this solution just downloads a text file with the binary content in it. How can I convert the binary data to the correspondent file type in the client side using JavaScript/Typescript? Thanks!

like image 339
Iñigo Avatar asked Sep 05 '25 16:09

Iñigo


1 Answers

You can use file-saver

import { saveAs } from 'file-saver';

const file = new Blob([content], {type: 'text/plain'});
FileSaver.saveAs(file, "test.txt");
like image 170
Adrita Sharma Avatar answered Sep 08 '25 12:09

Adrita Sharma