Skip to content

Instantly share code, notes, and snippets.

@blowery
Created November 19, 2009 20:58
Show Gist options
  • Save blowery/239043 to your computer and use it in GitHub Desktop.
Save blowery/239043 to your computer and use it in GitHub Desktop.
dojo.require("dijit._Widget");
dojo.declare("FileUploader", dijit._Widget, {
ID_KEY : 'APC_UPLOAD_PROGRESS',
statusUrl : 'status.php',
pollDelay : 500, // in milliseconds
form : null, // HTML form element
status : null, // element where the upload status is displayed
statusTemplate : null, // Prototype Template object
idElement : null, // element that holds the APC upload ID
iframe : null, // iframe we create that form will submit to
constructor : function(form, status)
{
// initialize the form and observe the submit element
this.form = dojo.byId(form);
this.connect(this.form, "onsubmit", "_onFormSubmit");
// create a hidden iframe
this.iframe = dojo.create('iframe', { name : '_upload_frame', style: { display: 'none' } });
// make the form submit to the hidden iframe
this.form.appendChild(this.iframe);
this.form.target = this.iframe.name;
// initialize the APC ID element so we can write a value to it later
this.idElement = this.form[this.ID_KEY];
// initialize the status container
this.status = dojo.byId(status);
// clear the status template
this._updateStatus();
},
generateId : function()
{
var now = new Date();
return now.getTime();
},
_onFormSubmit : function(e)
{
var id = this.generateId();
this.idElement.value = id;
this._monitorUpload(id);
},
_monitorUpload : function(id)
{
dojo.xhrPost({ url: this.statusUrl,
content: { id: id },
load: dojo.hitch(this, "_onMonitorSuccess"),
handleAs: "json"
});
},
_onMonitorSuccess : function(resp)
{
var json = resp;
dojo.style(this.status, "display", "block");
var theHtml = ...; // build your status html from the JSON
this._updateStatus(theHtml);
if (!json.finished) {
window.setTimeout(dojo.hitch(this, "_monitorUpload"), this.pollDelay);
}
},
_updateStatus: function(theHtml)
{
this.status.innerHTML = theHtml;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment