Search code examples
javascriptfilesystemsgoogle-chrome-app

How to convert FileEntry to standard JavaScript File object using chrome apps fileSystem


Is it possible to convert FileEntry to standard JavaScript object File? I can't find anything meaningful in documentation https://developer.chrome.com/apps/fileSystem


Solution

  • The FileEntry documentation does provide guidance here:

    The FileSystemFileEntry interface of the File System API represents a file in a file system. It offers properties describing the file's attributes, as well as the file() method, which creates a File object that can be used to read the file.

    Unfortunately file()method relies on callbacks rather Promises, but we can wrap that and make using the API (and reading the code) easier:

    async function getFile(fileEntry) {
      try {
        return new Promise((resolve, reject) => fileEntry.file(resolve, reject));
      } catch (err) {
        console.log(err);
      }
    }
    
    // From inside an async method or JS module
    let file = await getFile(fileEntry); // Wait until we have the file