Handling file uploads with JavaScript and Google Gears, is there a better solution?

Posted by gnarf on Stack Overflow See other posts from Stack Overflow or by gnarf
Published on 2010-02-18T21:00:52Z Indexed on 2010/03/22 11:41 UTC
Read the original article Hit count: 308

So - I've been using this method of file uploading for a bit, but it seems that Google Gears has poor support for the newer browsers that implement the HTML5 specs. I've heard the word deprecated floating around a few channels, so I'm looking for a replacement that can accomplish the following tasks, and support the new browsers. I can always fall back to gears / standard file POST's but these following items make my process much simpler:

  1. Users MUST to be able to select multiple files for uploading in the dialog.
  2. I MUST be able to receive status updates on the transmission of a file. (progress bars)
  3. I would like to be able to use PUT requests instead of POST
  4. I would like to be able to easily attach these events to existing HTML elements using JavaScript. I.E. the File Selection should be triggered on a <button> click.
  5. I would like to be able to control response/request parameters easily using JavaScript.

I'm not sure if the new HTML5 browsers have support for the desktop/request objects gears uses, or if there is a flash uploader that has these features that I am missing in my google searches.

An example of uploading code using gears:

// select some files:
var desktop = google.gears.factory.create('beta.desktop');
desktop.openFiles(selectFilesCallback);

function selectFilesCallback(files) {
  $.each(files,function(k,file) {
    // this code actually goes through a queue, and creates some status bars
    // but it is unimportant to show here...
    sendFile(file);
  });
}

function sendFile(file) {
  google.gears.factory.create('beta.httprequest');
  request.open('PUT', upl.url);
  request.setRequestHeader('filename', file.name);
  request.upload.onprogress = function(e) {
    // gives me % status updates...  allows e.loaded/e.total
  };
  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      // completed the upload!
    }
  };
  request.send(file.blob);
  return request;
}

Edit: apparently flash isn't capable of using PUT requests, so I have changed it to a "like" instead of a "must".

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about google-gears