Learning how to get files and file details from the file system is a basic data management skill:
To access a specific file or directory within the file system, retrieve a file handle using the resolve() method of the FileSystemManager interface:
webapis.filesystem.resolve('documents', onResolveSuccess, onResolveError, 'r');
The File object is returned in the success event handler.
- To retrieve a list of all the files and their directories located in a specified directory, use the listFiles() method of the File object:
function onResolveSuccess(dir) { dir.listFiles(onsuccess, onerror); }
The method returns an array of File objects.
To retrieve the file URI, use the toURI() method:
function onsuccess(files) { for (var i = 0; i < files.length; i++) { /* Display the file name and URL */ console.log("File name is " + files[i].name + "and URL is " + files[i].toURI());
To retrieve the file content as a DOMString, use the readAsText() method.
The encoding input parameter of the method defines the format in which the file content is returned.
if (files[i].isDirectory == false) files[i].readAsText(function(str) {console.log("File content: " + str);}, "UTF-8"); } }