创建JS二进制数据 [英] Create binary blob in JS
本文介绍了创建JS二进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我生成一个文件的客户端,我在十六进制数据,只想让用户下载生成的文件。
I'm generating a file client-side, I have the data in hexadecimal and just want to let the user download the generated file.
var blob = new Blob([hexData], {type: "application/octet-stream"});
console.log(URL.createObjectURL(blob));
得到的文件是一个包含ASCII十六进制数据的纯文本文件。我怎么能强迫的Blob包含二进制数据,而不要作为文本?
The resulting file is a plain-text file containing hex data in ASCII. How can I force the Blob to contain the binary data as is and not as text?
推荐答案
转换你的数据二进制数组,然后创建从一个blob。
Convert you data to a binary array then create a blob from that.
var byteArray = new Uint8Array(hexdata.length/2);
for (var x = 0; x < byteArray.length; x++){
byteArray[x] = parseInt(hexdata.substr(x*2,2), 16);
}
var blob = new Blob([byteArray], {type: "application/octet-stream"});
http://jsfiddle.net/mowglisanu/15h9o3d5/
这篇关于创建JS二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文