The Ashes
W3C publish first working draft of File API
The W3C has published a working draft for the File API which gives us a much improved <input type=”file”> and programmatic ability to work with file uploads and the like.
There are actually a few pieces to this work, which does a good job interfacing with other standards too:
This specification provides an API
for representing file objects in web applications, as well as programmatically selecting them and accessing their data. This includes:
- A FileList interface, which represents an array of individually selected files from the underlying system.
The user interface for selection can be invoked via<input type="file">
, i.e. when the
input
element [HTML5] is in the File Upload state, or through the FileDialog interface.
- A FileData interface, which provides asynchronous data accessors for file data via callback methods.
- A File interface, which includes readonly informational attributes about a file such as its name and its mediatype.
- A FileError interface, which defines the error codes used by this specification.
The API to get access to selected files is trivial (document.getElementById("myFileInput").files.length
etc) and then you can get the file data itself in various forms (data: URL, text, binary, Base64, new filedata:// URL).
An example usage of the filedata URL:
-
-
-
// Sample code in JavaScript
-
// Obtain fileList from <input type="file"/> using DOM
-
-
var file = fileList.files.item(0);
-
if (file)
-
{
-
// … Make asynchronous call
-
-
file.getAsURL(handleURL);
-
}
-
function handleURL(url, error)
-
{
-
if(url)
-
{
-
var img = new Image();
-
img.src = url;
-
-
// Other stuff…
-
-
}
-
else
-
{
-
// error conditions
-
}
-
}
-
Fun to see this all come together. The editor is a fellow Mozilla-n Arun Ranganathan … an all round good chap 🙂
Some have talked about alternative solutions such as using XHR to do the work, or DOM events to allow built-in progress events. The working group is listening, what would you like to see?
No Comments