Netvibes Ajax Uploader
Features
- Inspired by : Ajax Iframe method
- Dynamic Iframe declaration
- onStart & onComplete callback
- Multiple instance support
Code
Uploader = {}
Uploader.submit = function(form, options) {
var iframe = widget.createElement('iframe');
iframe.setStyle('display', 'none');
iframe.src = 'about:blank';
iframe.id = 'uploader' + Math.floor(Math.random() * 100000);
iframe.name = iframe.id;
iframe.onload = function() {
Uploader.loaded(this);
}.bind(iframe);
form.appendChild(iframe);
if (options && typeof(options.onComplete) == 'function') {
iframe.onComplete = options.onComplete;
}
form.target = iframe.name;
if (options && typeof(options.onStart) == 'function') {
return options.onStart();
} else {
return true;
}
}
Uploader.loaded = function(iframe) {
if (iframe.contentDocument) {
var doc = iframe.contentDocument;
} else if (iframe.contentWindow) {
var doc = iframe.contentWindow.document;
}
if (doc.location.href == "about:blank") {
return;
}
if (typeof(iframe.onComplete) == 'function') {
iframe.onComplete(doc.body.innerHTML);
}
}
Usage
var myform = widget.createElement('form');
myform.method = 'POST';
myform.enctype = 'multipart/form-data';
myform.action = 'upload/php';
myform.onsubmit = function() {
return Uploader.submit(this, {
onStart : function() {
alert('Upload start');
},
onComplete : function(msg) {
alert('Upload complete : ' + msg);
}});
}.bind(myform);