21

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

0

2 Answers 2

30

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
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, I was curious why you used async and await when defining getFile. Would it not work the same if you got rid of them, and just returned the promise directly?
@ack_inc yes you're right, it doesn't add anything, thanks for picking that up. Edited per your comment.
3

I found how to do this in google examples https://github.com/GoogleChrome/chrome-app-samples/blob/master/samples/filesystem-access/js/app.js

var jsFileObject;
fileEntry.file(function (file){ 
   jsFileObject = file 
});

2 Comments

Don't forget the error callback! You will silently never get a callback in some (albeit) rare cases. The signature should be entry.file(success,error)
Take note that this code is asynchronous: you can't immediately use jsFileObject. See xlm's answer for a Promise-based solution to that problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.