admin管理员组文章数量:1252939
I have a typed array full of binary data that is being generated from an ArrayBuffer
var myArr = new Uint8Array(myBuffer);
I am presenting this to the user with
var blob = new Blob(myArr, {type: "octet/stream"};
var blobURL = URL.createObjectURL(blob);
and inserting a link that is
"<a href=" + blobUrl + " download=" + filename "/a>"
Later, I am letting the user select the file from disk, and using a file reader to do with
var reader = new FileReader();
reader.onload = function () {
console.log(reader.result);
};
reader.readAsArrayBuffer(sourceFile);
The problem is, it seems like no matter what I do, I get a "string" of the file's contents. In fact, when I save the file, I can open it, and it is plainly human readable. I.E. if my Uint8Array was {"0" : "51", "1" : "52", "2" : "53" }
I can open the downloaded blob in a text editor and I just see "515253" which I don't think is what should be happening.
How can I make a link to a file download for my file that is formatted properly so I can read it back in an dget the right values?
I have a typed array full of binary data that is being generated from an ArrayBuffer
var myArr = new Uint8Array(myBuffer);
I am presenting this to the user with
var blob = new Blob(myArr, {type: "octet/stream"};
var blobURL = URL.createObjectURL(blob);
and inserting a link that is
"<a href=" + blobUrl + " download=" + filename "/a>"
Later, I am letting the user select the file from disk, and using a file reader to do with
var reader = new FileReader();
reader.onload = function () {
console.log(reader.result);
};
reader.readAsArrayBuffer(sourceFile);
The problem is, it seems like no matter what I do, I get a "string" of the file's contents. In fact, when I save the file, I can open it, and it is plainly human readable. I.E. if my Uint8Array was {"0" : "51", "1" : "52", "2" : "53" }
I can open the downloaded blob in a text editor and I just see "515253" which I don't think is what should be happening.
How can I make a link to a file download for my file that is formatted properly so I can read it back in an dget the right values?
Share Improve this question asked May 2, 2016 at 14:52 DerekDerek 11.9k33 gold badges135 silver badges235 bronze badges3 Answers
Reset to default 18As it turns out, the problem was that I had a syntax error in the creation of the Blob.
The corrected code looked like:
var blob = new Blob([myArr], {type: "octet/stream"});
I'm not really sure why if I am already passing an ArrayBuffer argument. Why I need bracket notation? Seems redundant?
According to Mozilla
https://developer.mozilla.org/en-US/docs/Web/API/Blob#Example_for_creating_a_URL_to_a_typed_array_using_a_blob
var blob = new Blob([typedArray.buffer], {type: 'application/octet-stream'});
const download = function (data)
{
const blob = new Blob(data, { type: "octet/stream"});
const url = window.URL.createObjectURL(blob)
const a = document.createElement('a')
a.setAttribute('href', url)
a.setAttribute('download', app.lastSub+"_"+new Date().toISOString()+'.py');
a.click()
}
const get = async function () {
download(app.psspyList);
}
本文标签: JavascriptSave typed array as blob and read back in as binary dataStack Overflow
版权声明:本文标题:Javascript - Save typed array as blob and read back in as binary data - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738300869a2073600.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论