79244499

Date: 2024-12-02 14:51:23
Score: 1
Natty:
Report link

Update 2024:

While it is still not possible to serialize the actual File Object, a way more efficient solution to store the content for later upload (compared to serializing to base64) is possible thanks to the widely avaible origin private file system* (OPFS).

Below is a minimal Typescript example to store an array of File objects with cacheFiles and retrieving them with retrieveFiles:

public async cacheFiles(files: File[]) Promise<void> {
  const opfs = await navigator.storage.getDirectory();
  for (let file of files) {
    const handle = await opfs.getFileHandle(file.name, {create: true});
    const stream = await handle.createWritable();
    await stream.write(file);
    await stream.close();
  }
}

private async retrieveFiles(): Promise<File[]> {
  const opfs = await navigator.storage.getDirectory();
  const files: File[] = [];
  for await (const fileEntry of opfs.values()) {
    const fileHandle = await opfs.getFileHandle(fileEntry.name);
    files.push(await fileHandle.getFile());
  }
  return files;
}

The retrieved array of Files can then be used just like the FileList returned by the files attribute of HTMLInputElement with type=file. Since 2023 the methods used in above example are available in all major browsers (MDN Baseline 2023).

An extensive guide with examples can be found at https://developer.mozilla.org/en-US/docs/Web/API/File_System_API/Origin_private_file_system .

Working with a File reference without copying the content, as described at https://developer.chrome.com/docs/capabilities/web-apis/file-system-access , should become possible once the File System Access API (https://wicg.github.io/file-system-access/#api-showopenfilepicker) is widely supported, namely the methods

As of December 2024, this is not the case!

Reasons:
  • Blacklisted phrase (0.5): thanks
  • Probably link only (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: rorymichele