Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
50
rated 0 times [  52] [ 2]  / answers: 1 / hits: 15223  / 5 Years ago, wed, july 17, 2019, 12:00:00

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!


More From » angular

 Answers
50

You can use file-saver



import { saveAs } from 'file-saver';



const file = new Blob([content], {type: 'text/plain'});
FileSaver.saveAs(file, test.txt);

[#51867] Thursday, July 11, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaleyv

Total Points: 259
Total Questions: 99
Total Answers: 107

Location: Saint Helena
Member since Tue, Nov 3, 2020
4 Years ago
;