JavaScript/Working With Files

From Wikibooks, open books for an open world
Jump to: navigation, search

With pure HTML4 and pure JavaScript, there's really only one thing you can do with the users files: the server sends a web page that includes a form something like this:[1]

<form action="/upload_handler" method="post">
    <input type="file" />
</form>

and then the browser allows the user to select one file, and the browser uploads it -- without any JavaScript on the client being able to see any of that data or cancel the transmission or even show a progress bar.

If you want JavaScript to know anything about the file before it is transmitted (for example, to immediately cancel the transmission of a large file, rather than wait an hour for the file to be transmitted, *then* tell the user "File too large"; or to show a progress bar), you'll have to use something other than pure JavaScript on pure HTML4. Some popular options are:[2]

  • use a modern web browser that supports the HTML5 File API.
  • use Flash (perhaps a tiny flash utility like Gmail uses to draw a little progress bar)
  • use a Java applet
  • use an ActiveX control

References[edit]