[whatwg] Proposal: API to ask the user for a file

JC mg05182-wwg at yahoo.ca
Mon Apr 29 10:53:08 PDT 2013


Hi,

So far the only way to ask a user to select a file (e.g. to upload an attachment in a mail client) without showing the ugly "file input" UI is to create one of these elements, hide it somehow, and invoke the "click" method on it and listen for the "changed" event. This may or may not work depending on the browser and version and there's no way to know this other than doing extensive tests. Ideally this, rather common task, should have an official API instead of this hack.

The minimum requirements would be to be able to provide a string for the dialog title, a string for the accepted mime types and a boolean to specify whether multiple files can be selected at once, the return value would be a FileList object [1]. This is essentially the same functionality offered by the existing file input element [2] with the only addition of a title for the dialog.

A very simple way to provide this functionality would be to add a method to HTMLDocument like this:

FileList openFileDialog(DOMString title, DOMString accept, boolean multiple);

On success the method would return the list of selected files or null if the used pressed cancel on the dialog. If the user agent can't show the dialog for some reason the method should throw an exception so the developer has the opportunity to do something in this case.

Of course there are other options to provide the same functionality, like creating an object to represent the dialog which would allow for asynchronous processing by using callbacks to notify the selected files.

One specific request which comes from the project in which I'm currently working is to allow the use of this API from the callbacks of XMLHttpRequest and WebSocket to make possible to have the UI logic on the server, i.e. when the user clicks on a button a message is sent to the server, the business logic in the server defines the behaviour for the button pressed and sends a notification back to the client about what to do which results in this API being invoked.

1. http://dev.w3.org/2006/webapi/FileAPI/#dfn-filelist
2. http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#concept-input-type-file-selected

Thank you.

-- 
JC


More information about the whatwg mailing list