I want to download some binary data but Chrome forces it to utf-8 which messes up the bytes. If I set href to a data URI and use base64 encoding it works, but I want to avoid that and use a BLOB instead.
Is there a way to stop Chrome from converting this binary data to utf-8?
const data = "\x00\xff\xfe\xe2"
console.log(data)
const file = new Blob([data])
const a = document.getElementById("a")
const url = URL.createObjectURL(file)
a.href = url;
a.download = "test.txt";
<a id="a">Download</a>
In this example, you can see that my string contains the bytes
00 ff fe e2
and I convert that string to a blob. But when you download the file in Chrome and look at the bytes again you'll find
20 c3 bf c3 be c3 a2
instead.
What I think is happening is that chrome takes each byte, reads it as utf-16 and converts it to utf-8. For example fe is þ when read as utf-16 but c3 be in utf-8