HTML and CSS Reference
In-Depth Information
Working with Files
When writing desktop applications, the ability to read and write files from the user's file-
system is taken for granted. Once a client application is installed on a computer, it largely
has full access to all the files the user can access.
Web applications running inside a web browser are naturally restricted in how they can ac-
cess the users file-system. Users would naturally be very nervous if any web site could read
files on their file-system.
Since its early days, HTML has supported a file form element that allowed the user to select
a file from their file-system, and post its contents to the server. Even this was subject to tight
restrictions: it was not possible to interact with this field via JavaScript in any way, includ-
ing accessing the contents of a file that the user had selected.
HTML5 includes several APIs for interacting with the users file-system. The most ambitious
of these is the File System and File Writer APIs. These specifications have been proposed by
Google and are currently only supported in Chrome. They allow a web application to read
files, create directories and write files in a virtual file-system.
The word “virtual” is the key here. Each origin will be given its own virtual file-system that
it has full control over, but this will be partitioned from both the users real file-system, and
the file-system of any pages loaded from any other origin. In addition, depending on how
the API is implemented, the virtual file-system may not exist as a set of files and directories
on the users file-system (it could be implemented as a binary database).
As a result of the other storage options we have examined in earlier chapters, these APIs are
of limited use, and you may be advised to continue using the other storage options we have
discussed.
The other major file related API is the FileReader API. This is a simpler, but in many ways
more interesting API. It relies on the HTML file form field, but once a file is selected it
provides the developer access to the file contents in JavaScript. This means it is possible to
perform local parsing of a file without ever submitting it to the web server.
This has several advantages:
1. The user can load files when they are offline (provided the web application has been made
to work offline using the other techniques outlined in this topic).
2. The contents of a file could be submitted to the server using an AJAX call rather than a
server post.
Search WWH ::




Custom Search