Local file system

Web.dev.app's file API was inspired by ActionScript's file API. The concepts are similar, but the properties and methods are simpler or improved. This API is not available to pages loaded from remote servers.

The following schemes are supported for resolving a resource path...

asset://filepath relative to the homepage folder, either on disk or inside a packaged app
private://filepath relative to the app's private data folder
shared://filepath relative to the app's shared data folder
app://package/filepath used internally to resolve more than one asset package. Use asset:// instead.

Filenames on Windows should use forward slashes. If no scheme is given, and the name appears to be a local filename, then it will be assumed to be a local filename. Different operating systems handle private and shared data in their own way.

Packaged apps have a home page, but pages displayed in the Browser tab of web.dev.app do not know which is the homepage folder of your app, so they cannot resolve the asset:// scheme, unless you include a <meta home> tag in your home page, which sets an internal home folder variable.

Extra String methods for filenames

Web.dev.app includes extra methods in String.prototype for manipulating strings containing filenames or URLs. They were added to simplify the development of web.dev.app's interface.

string.setExtension ("extension")

string.setFolder ("folder pathname")

string.shortenURL (length)

string.sliceFileName ()

string.sliceFileTitle ()

string.sliceExtension ()

string.sliceFolder ()

Properties of the dev.file.System constructor

The dev.file.System constructor has static properties for getting information about the file system...

dev.file.System.drives

dev.file.System.privateAppData

dev.file.System.sharedAppData

Properties of a dev.file.Drive object

A disk drive can be referenced using a dev.file.Drive object. Any file or folder can be given as an argument... var drive_from_path = new dev.file.Drive ("pathname"); var drive_from_node = new dev.file.Drive (dev.file.Node);

drive.freeSpace

drive.isRemovable

drive.isWritable

drive.name

drive.path

drive.root

drive.size

Properties of a dev.file.Node object

Files and folders are both considered to be nodes in the file system, and both can be referenced using dev.file.Node objects. Packaged assets can be referenced too using asset://, but because packaged assets are read-only, only a limited number of operations can be applied to them. var node = new dev.file.Node ("file or folder pathname");

node.children

node.created

node.exists

node.extension

node.folder

node.isFolder

node.isHidden

node.modified

node.name

node.path

node.parent

node.size

Methods of a dev.file.Node object

The following methods do not wait for the operation to be performed. The operation is placed on a queue to be performed asyncronously by a separate thread. Each of these methods returns a promise object which resolves when the operation either finishes or fails. Methods with a pattern argument can accept strings with wildcards ? and * to match any single character or group of characters. Future versions may accept regular expressions as patterns. For example... node.deleteFiles ('*.tmp', true).then (function(){ console.log ('done');});

... asyncronously deletes all .tmp files in a folder tree and logs 'done' when complete.


node.append (data)

node.copyTo ("file or folder pathname")

node.copyFilesTo ("folder pathname")
node.copyFilesTo ("folder pathname", pattern)
node.copyFilesTo ("folder pathname", pattern, deep)

node.delete ()

node.deleteFiles ()
node.deleteFiles (pattern)
node.deleteFiles (pattern, deep)

node.deleteFolders ()
node.deleteFolders (pattern)
node.deleteFolders (pattern, deep)

node.findFiles ()
node.findFiles (pattern)
node.findFiles (pattern, deep)

node.findFolders ()
node.findFolders (pattern)
node.findFolders (pattern, deep)

node.makeFolder ()

node.moveTo (path)

node.moveFilesTo ("folder pathname")
node.moveFilesTo ("folder pathname", pattern)
node.moveFilesTo ("folder pathname", pattern, deep)

node.readAsArrayBuffer ()

node.readAsDataURL ()

node.readAsText ()
node.readAsText (encoding)

node.rename ("new name")

node.write (data)


The following method can be used to syncronise the file operation queue...

dev.file.System.wait ()

Methods of a dev.file.Dialog object

The methods of dev.file.Dialog will open file selection dialogs. They return promise objects which are either fulfilled with a dev.file.Node object or rejected if the dialog is cancelled. var dialog = new dev.file.Dialog; dialog.browseForOpen().then (function (node){ console.log (node.path);});


dialog.browseForOpen ()
dialog.browseForOpen (options)

dialog.browseForSave ()
dialog.browseForSave (options)


The optional options argument expects an object with one or more of the following properties... { title: "dialog box title", select: "select button text", cancel: "cancel button text", path: "current filename" }

For example... dialog.browseForOpen ({ title: "Choose a file", path: old_file }).then...

Future versions will have folder selection and multiple file selection dialogs.

Example - copyTo
var file = new dev.file.Node ('c:/temp/original.file'); file.copyTo ('copied.file'); // copies to c:/temp/copied.file file.copyTo ('c:/copy'); // copies to c:/copy/original.file var folder = new dev.file.Node ('c:/temp'); folder.copyTo ('c:/deep/copy').then (function (){ // c:/deep/copy is now a deep copy of c:/temp }); file.delete(); // deletes original.file

Async file operations are queued, so even though file.delete() is called while the folder may still be copying, the delete wont be performed until after the copy is finished. Use dev.file.System.wait() to pause until all async operations have been performed.

Example - read and write
var file = dev.file.Node ('c:/temp/sample.txt'); // write to and read from the file file.write ('some sample text').then (function (){ return file.readAsText(); }).then (function (text){ console.log (text); });