W3C home > Mailing lists > Public > public-webapps@w3.org > July to September 2009

Re: Alternative File API

From: Garrett Smith <dhtmlkitchen@gmail.com>
Date: Sat, 15 Aug 2009 09:19:48 -0700
Message-ID: <c9e12660908150919n7db0b5cfs39a4bfa7b8788d67@mail.gmail.com>
To: Jonas Sicking <jonas@sicking.cc>
Cc: Webapps WG <public-webapps@w3.org>
On Tue, Aug 11, 2009 at 7:20 PM, Jonas Sicking<jonas@sicking.cc> wrote:
> Here is an alternative proposal for an API for reading files:

[snip proposal]

> As stated, I'm not convinced that this is a better solution than what
> the spec currently does. The only advantage I can see is that it
> supports progress events without requiring the use of XMLHttpRequest,
> and the downside is that it's a significantly bigger API. Usage
> examples would be:

The advantage to having the callback as an event is that it lets
multiple callbacks be attached easily to the read.

The File can be found in the DOM as:-

var files = input.files,
    file =  files && files[0];

That file might be gotten from two separate unrelated parts of the
code. Each could a callback to that file and issue different getXXX
commands to it, creating a race condition whereby one callback could
get called where it was expecting the payload to have text and another
callback to get called where it was expecting a payload of binary

Instead, a separate reader can be obtained to read the file and that
reader can have the callback.

> Current draft:
> myFile.getAsBinaryString(handler);
> function handler(data, error) {
>  doStuffWith(data);
> }
> Above API:

// Is it "FileRequest" or "FileReader"?
> reader = new FileReader;

// The following two statements are backwards.
> reader.readAsBinaryString(myFile);
> reader.onload = handler;
> function handler(event) {
>  doStuffWith(event.target.response);
> }

What happens when the reader is in process of reading?
var reader = new FileReader;
reader.onload = handler;

The callback would have to know in advance what type of read happened.
So you'd want to have a different reader for each type of read. For

var bReader = new FileReader;
bReader.onload = handler;
var tReader = new FileReader;

As you can see, the read /type/ is exclusive to the reader. "tReader"
is only reading "text" and "bReader" is only reading binary. Each
reader reads only one type. So doesn't a Factory seem more appropriate
than a bunch of constructors?

var bReader = FileReader.create(FileReader.BINARY); // (the "etc" part).
bReader.onload = handler;

> / Jonas

Received on Saturday, 15 August 2009 16:20:28 UTC

This archive was generated by hypermail 2.3.1 : Friday, 27 October 2017 07:26:18 UTC