Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 428
Michał Jereczek Avatar asked Jul 12 '17 08:07

Michał Jereczek


2 Answers

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 await new Promise((resolve, reject) => fileEntry.file(resolve, reject));
  } catch (err) {
    console.log(err);
  }
}

var file = await getFile(fileEntry);

Note that FileSystemFileEntry is non-standard feature and is not a standards track, so the API and browser support may change!

like image 66
xlm Avatar answered Oct 04 '22 21:10

xlm


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 
});
like image 44
Michał Jereczek Avatar answered Oct 04 '22 22:10

Michał Jereczek