Infrequently Noted

Alex Russell on browsers, standards, and the process of progress.

File Upload...without the file?

In a previous post I outlined how easy it is to upload files using Dojo's pluggable I/O system. By including the IframeIO package, Dojo makes sending forms containing file upload elements is just as easy as making any other Ajax request.

But what about when you want to send data as though it were a file being uploaded, but you don't actually want to pull a file off the disk? Eugene Lazutkin needed exactly this and worked up patches which are now a part of Dojo. Admittedly it's not something you need all the time. After all, you've been making due without it for years, right? But there are many situations where being able to treat the browser as a file editor for plain-text content makes sense, and sending content pre-encoded as files can help.

The default XMLHTTP transport class supports this unique feature through the file argument. The system expects fileto be an object with 3 properties:

From here it's easy to construct a request that uploads some content as a file:{
  url: "upload.cgi",
  // we expect to get JSON back from this CGI
  mimetype: "text/json",
  // handle the returned JSON
  load: function(type, data, evt){  },
  // normal properties
  content: {
    foo: "bar",
    baz: "thud"
  // our file content
  file: {
    name: "upload.txt",
    contentType: "plain/text",
    content: "look ma! no form node!"
From here all of the mime-encoding magic is handled for you.

File uploading without the file: just one more reason to drop that 10-line one-off XMLHTTP wrapper and pick up something better.