I would like to read and write to a large file (stored on the user's file system) using browser Javascript without loading the whole file into RAM (it's too big to fit in RAM). I already have the code that lets me read a section of the file without loading the file into RAM, but I can't find how to write to the file.
class DiskAdapter {
file;
constructor(file) {
this.file = file;
}
async read(address, len) {
let diskFile = this.file;
return new Promise((resolve) => {
let reader = new FileReader();
reader.onload = function() {
resolve(reader.result);
};
let blob = diskFile.slice(address, address + len);
reader.readAsArrayBuffer(blob);
});
}
async write(address, len, data) {
// what goes here?
}
}
document.getElementById("diskImage").addEventListener('change', (changeEvent) => {
let diskFile = changeEvent.target.files[0];
let diskAdapter = new DiskAdapter(diskFile);
console.log(await diskAdapter.read(0, 512)); // read MBR of disk image
});
My goal is to have a disk image (a .iso or .img file) that can be booted in a virtualized OS in a browser. The user can then perform tasks in the OS and the changes will automatically be saved to persistent storage. And I want the disk image to be easily swappable (the user can easily boot from a different iso or copy the iso to a different browser).