简体   繁体   English

Javascript使用ANSI编码将二进制数据保存到文件

[英]Javascript save binary data to file with ANSI encoding

I am trying to save a binary string to file using Filesaver.js . 我正在尝试使用Filesaver.js将二进制字符串保存到文件中。 I am specifying the charset as ANSI but the file has the UTF-8 encoding. 我将字符集指定为ANSI,但是文件具有UTF-8编码。

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

Is there a way to save the file as ANSI? 有没有一种将文件另存为ANSI的方法?

Are you sure bin is ANSI data? 您确定bin是ANSI数据吗? I had the same problem where I wanted to download a zip file from the server. 我在要从服务器下载zip文件时遇到了同样的问题。 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: 这是我在Angular中下载zip文件的代码示例,但是无论您如何发出http请求,它都是相同的概念:

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

Then you can create the Blob and save it: 然后,您可以创建Blob并将其保存:

var blob = new Blob([bin], { type: "application/zip", responseType: 'arraybuffer' });
saveAs(blob, "binfile.zip");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM