Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript save binary data to file with ANSI encoding

I am trying to save a binary string to file using Filesaver.js. I am specifying the charset as ANSI but the file has the UTF-8 encoding.

var blob = new Blob([bin], {type: "octet/stream;charset=ANSI"});
saveAs(blob, "binfile.dat");

Is there a way to save the file as ANSI?

like image 324
Marin Bînzari Avatar asked Nov 09 '22 22:11

Marin Bînzari


1 Answers

Are you sure bin is ANSI data? I had the same problem where I wanted to download a zip file from the server. It turns out that I had to specify in the header of my request what response I wanted. Here's my code sample in Angular downloading a zip file but it will be the same concept no matter how you make your http request:

$http.get(url, { responseType: 'arraybuffer' })

Then you can create the Blob and save it:

var blob = new Blob([bin], { type: "application/zip", responseType: 'arraybuffer' });
saveAs(blob, "binfile.zip");
like image 181
Doug Avatar answered Nov 14 '22 22:11

Doug