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:

  • name – the filename
  • contentType – optional, defaults to “application/octet-stream”
  • content – the content of your file as a string

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.


  1. Posted January 9, 2006 at 12:26 pm | Permalink

    And i have not known that! Thanks for opening my eyes. And for the link.

  2. J Wilks
    Posted March 10, 2006 at 1:49 pm | Permalink

    What if you just want to send raw data? We have a “legacy AJAX” app that sends straight XML up to the server via XmlHttpRequest — but with I only see how to send form parameters and multipart/files… any way to access the “raw data” that will be sent?

  3. Raman Basu
    Posted July 4, 2006 at 8:39 pm | Permalink

    There is not enough information about how to handle, upload and process multipart-formdata oriented file uploads. What I have found as example is :

    file: {
    name: “upload.txt”,
    contentType: “plain/text”,
    content: “look ma! no form node!”
    But who will process the upload, what about multiple files???

One Trackback

  1. […] He then posts about file upload… without the file!, which shows how you can send any stream of content up as though it is a file upload, using the XMLHttpTransport and: […]