source: [view]
dojo.provide("dojox.form.uploader.plugins.HTML5");
dojo.declare("dojox.form.uploader.plugins.HTML5", [], {
//
// Version: 1.6
//
// summary:
// A plugin for dojox.form.Uploader that adds HTML5 multiple-file upload capabilities and
// progress events.
//
// description:
// Add this plugin to have HTML5 capabilities in the Uploader. Note that it does not add
// these capabilities to browsers that don't support them. For IE or older browsers, add
// additional plugins: IFrame or Flash.
//
errMsg:"Error uploading files. Try checking permissions",
// Overwrites "form" and could possibly be overwritten again by iframe or flash plugin.
uploadType:"html5",
postCreate: function(){
this.connectForm();
this.inherited(arguments);
if(this.uploadOnSelect){
this.connect(this, "onChange", "upload");
}
},
/*************************
* Public Methods *
*************************/
upload: function(/*Object ? */formData){
// summary:
// See: dojox.form.Uploader.upload
//
this.onBegin(this.getFileList());
if(this.supports("FormData")){
this.uploadWithFormData(formData);
}else if(this.supports("sendAsBinary")){
this.sendAsBinary(formData);
}
},
submit: function(/* form Node ? */form){
// summary:
// See: dojox.form.Uploader.submit
//
form = !!form ? form.tagName ? form : this.getForm() : this.getForm();
var data = dojo.formToObject(form);
console.log("form data:", data);
this.upload(data);
},
sendAsBinary: function(/* Object */data){
// summary:
// Used primarily in FF < 4.0. Sends files and form object as binary data, written to
// still enable use of $_FILES in PHP (or equivalent).
// tags:
// private
//
if(!this.getUrl()){
console.error("No upload url found.", this); return;
}
// The date/number doesn't matter but amount of dashes do. The actual boundary
// will have two more dashes than this one which is used in the header.
var boundary = "---------------------------" + (new Date).getTime();
var xhr = this.createXhr();
xhr.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
// finally send the request as binary data
// still accessed as $_FILES
var msg = this._buildRequestBody(data, boundary);
if(!msg){
this.onError(this.errMsg);
}else{
xhr.sendAsBinary(msg);
}
},
uploadWithFormData: function(/* Object */data){
// summary
// Used with WebKit and Firefox 4+
// Upload files using the much friendlier FormData browser object.
// tags:
// private
//
if(!this.getUrl()){
console.error("No upload url found.", this); return;
}
var fd = new FormData();
dojo.forEach(this.inputNode.files, function(f, i){
fd.append(this.name+"s[]", f);
}, this);
if(data){
for(var nm in data){
fd.append(nm, data[nm]);
}
}
var xhr = this.createXhr();
xhr.send(fd);