How do you append data to a file?

Currently working on upgrading my encryption add-on to include support for files. I’ve currently been able to make it work by encrypting the file data and then creating a Blob that the user can “download”.

The issue is with large files. Naturally, because Firefox needs to load all of the incoming file’s contents into memory and then encrypt it, it’s causing the browser to crash. I’m currently using a FileReader() to read the file, so my plan is to only read chunks and encrypt those chunks individually. Then, it can be appended together and “downloaded” as a single file.

But I think I will still run into the issue with low memory because it still needs to all be loaded into memory. So, I was wondering if there was a way for an extension to append data to a file, without having to read the contents, store it in data, append it and then re"download" it?

The only other implementation I can think of is to push each string into the Storage API and then grab it all after and then append it into a Blob that can be downloaded as a single file, but that doesn’t seem like a great implementation.

1 Like

With the IDBFileStorage stuff you can in theory append:


You can get a LockedFile from a IDBMutableFile. See also https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Working_with_files#Store_files_data_locally_using_the_IndexedDB_file_storage_library

I’m not sure if this allows you to then offer to download a file without loading it into memory all at once, but it will let you append to a file.