Files and data

Keep binary files as bytes until decoding is justified

Read binary content as bytes rather than assuming every selected file is text. Text decoding is a separate interpretation step and can replace byte sequences that are not valid in the chosen encoding.

Worked example

Node.js example · save as example.mjs and run node example.mjs

const file = new Blob([new Uint8Array([0, 255, 65])], {type: "application/octet-stream"});
const bytes = new Uint8Array(await file.arrayBuffer());
console.log([...bytes].join(","));
console.log(bytes.length);

How it works

ArrayBuffer returns the stored byte content, and Uint8Array provides convenient byte indexing. The file’s bytes remain 0, 255, and 65. This is the right representation for hashing, copying, or inspecting a binary signature.

Limits and mistakes

Reading the entire Blob still allocates memory proportional to its size. Use slicing or streams where needed. A MIME label supplied by the browser or filename does not prove that the bytes follow that format.

Check the result

Expect 0,255,65 and 3. Round-trip those bytes through your storage or transfer path and compare byte values, not only a decoded text display.

Sources

Reference documentation for the behavior used in this guide: