Client-side file handlingThe SiteFusion client is based on webbrowser technology, but unlike a web-application there are no restrictions in accessing files and running executables on the client-side. Through the ClientFileService class all these actions can be initiated. The ClientFileService class is a Node, and thus needs to be added to the node tree of your application to be able to function:
All methods of ClientFileService are asynchronous. This means that they will not block your code but instead rely on a callback mechanism to inform you when the operation ended and what the result was. The callback is not required, which could make sense in the above example, if the caller would not care whether the writing of the file was successful. However when reading anything, files or directory listings, you have to specify a callback to receive the data.
The callback function could then look like this:
the $fileToStringUploader variable in the above example function refers one of the several uploader and downloader classes that the file handling API contains. These classes are used to manage longer operations separately and to inform the program on progress of the operation. They are constructed by the ClientFileService and returned from the associated functions readFile, readFileToStream, readFileToString, writeFile, writeFileFromStream and writeFileFromString.
The downloader classes are FileDownloader, FileFromStreamDownloader and FileFromStringDownloader. The uploader classes are FileUploader, FileToStreamUploader and FileToStringUploader. These objects all fire the following events:
Working with directories
Client-side directories can be read and have a serverside representation in the ClientDirectory class. The files and directories they contain are placed in the ClientDirectory::$entries array as ClientFile and ClientDirectory objects. It's easy to iterate over the contents of a directory:
Sub-directories are not read recursively. To get the contents of a sub-directory, call the ClientDirectory::get() method.
To create a new directory, you first need to create the ClientDirectory representation of this directory, and then call the ClientDirectory::create() method:
Similarly, directories and files can be removed my calling the ClientFile::remove() and ClientDirectory::remove() methods, or through the ClientFileService directly with ClientFileService::removeDirectory() and ClientFileService::removeFile().
Executing and monitoring files
Executable files can be launched through the ClientFileService as well. The direct method is by calling ClientFileService::executeFile(). When in a context of having a ClientFile at hand, it can be executed by calling the ClientFile::execute() method. In both cases, the execute method can take an optional arguments array $args, which can be supplied as an empty array to call the executable without arguments, and a boolean $async, which indicates whether the application should block (FALSE) or continue (TRUE) until the executable ends.
Monitoring files can be similarly achieved by calling ClientFile::monitor() or ClientFileService::monitorFile(). These methods require a callback handler to be specified. This callback handler get called whenever the state of the file changes. It is supplied with information on whether the file exists, what size it is and what its modification time is.
Using the native XULFilePicker
XUL supplies the file picker to enable users to select single or multiple files and directories on their local filesystem. This filepicker is featured as the OS-native file picker on all operating systems. In the constructor of XULFilePicker, the title, picker mode, default path and default filename extension can be specified. Picker mode is a string and should be one of the following:
The filepicker fires a closed event when it is dismissed. Attach an event handler to this event to initiate action:
Filters can be added to a XULFilePicker before it is opened in order to determine what file types and extensions are to be shown in the directory views and as the format options for saving.
|All content is © 2009 by FrontDoor Media Group, is available under the Creative Commons Attribution-Share Alike 2.5 License and is maintained by the SiteFusion.org staff|