| 1 |
define([ |
| 2 |
"dojo/_base/declare", |
| 3 |
"dojo/_base/lang", |
| 4 |
"dojo/_base/array", |
| 5 |
"dojo", |
| 6 |
"dojo/request", |
| 7 |
"dojo/has" |
| 8 |
],function(declare, lang, arrayUtil, dojo, request, has){ |
| 9 |
return declare("dojox.form.uploader._HTML5", [], { |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
errMsg:"Error uploading files. Try checking permissions", |
| 21 |
|
| 22 |
|
| 23 |
uploadType:"html5", |
| 24 |
|
| 25 |
postMixInProperties: function(){ |
| 26 |
this.inherited(arguments); |
| 27 |
if(this.uploadType === "html5"){ } |
| 28 |
}, |
| 29 |
|
| 30 |
postCreate: function(){ |
| 31 |
this.connectForm(); |
| 32 |
this.inherited(arguments); |
| 33 |
if(this.uploadOnSelect){ |
| 34 |
this.connect(this, "onChange", function(data){ |
| 35 |
this.upload(data[0]); |
| 36 |
}); |
| 37 |
} |
| 38 |
}, |
| 39 |
|
| 40 |
_drop: function(e){ |
| 41 |
dojo.stopEvent(e); |
| 42 |
var dt = e.dataTransfer; |
| 43 |
this._files = dt.files; |
| 44 |
this.onChange(this.getFileList()); |
| 45 |
}, |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
upload: function( formData){ |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
this.onBegin(this.getFileList()); |
| 55 |
this.uploadWithFormData(formData); |
| 56 |
}, |
| 57 |
|
| 58 |
addDropTarget: function(node, onlyConnectDrop){ |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
if(!onlyConnectDrop){ |
| 69 |
this.connect(node, 'dragenter', dojo.stopEvent); |
| 70 |
this.connect(node, 'dragover', dojo.stopEvent); |
| 71 |
this.connect(node, 'dragleave', dojo.stopEvent); |
| 72 |
} |
| 73 |
this.connect(node, 'drop', '_drop'); |
| 74 |
}, |
| 75 |
|
| 76 |
uploadWithFormData: function( data){ |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
if(!this.getUrl()){ |
| 84 |
console.error("No upload url found.", this); return; |
| 85 |
} |
| 86 |
var fd = new FormData(), fieldName=this._getFileFieldName(); |
| 87 |
arrayUtil.forEach(this._files, function(f, i){ |
| 88 |
fd.append(fieldName, f); |
| 89 |
}, this); |
| 90 |
|
| 91 |
if(data){ |
| 92 |
data.uploadType = this.uploadType; |
| 93 |
for(var nm in data){ |
| 94 |
fd.append(nm, data[nm]); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
var self = this; |
| 99 |
var deferred = request( |
| 100 |
this.getUrl(), |
| 101 |
{ |
| 102 |
method: "POST", |
| 103 |
data: fd, |
| 104 |
handleAs: "json", |
| 105 |
uploadProgress: true, |
| 106 |
headers: { |
| 107 |
Accept: "application/json" |
| 108 |
} |
| 109 |
}, |
| 110 |
true |
| 111 |
); |
| 112 |
|
| 113 |
deferred.promise.response |
| 114 |
.otherwise(function (error){ |
| 115 |
console.error(error); |
| 116 |
console.error(error.response.text); |
| 117 |
self.onError(error); |
| 118 |
}) |
| 119 |
; |
| 120 |
|
| 121 |
function onProgressHandler(event){ |
| 122 |
self._xhrProgress(event); |
| 123 |
|
| 124 |
if(event.type !== "load"){ |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
self.onComplete(deferred.response.data); |
| 129 |
|
| 130 |
|
| 131 |
deferred.response.xhr.removeEventListener("load", onProgressHandler, false); |
| 132 |
deferred.response.xhr.upload.removeEventListener("progress", onProgressHandler, false); |
| 133 |
|
| 134 |
deferred = null; |
| 135 |
} |
| 136 |
|
| 137 |
if(has("native-xhr2")){ |
| 138 |
|
| 139 |
deferred.response.xhr.addEventListener("load", onProgressHandler, false); |
| 140 |
deferred.response.xhr.upload.addEventListener("progress", onProgressHandler, false); |
| 141 |
}else{ |
| 142 |
|
| 143 |
deferred.promise.then(function(data){ |
| 144 |
self.onComplete(data); |
| 145 |
}); |
| 146 |
} |
| 147 |
}, |
| 148 |
|
| 149 |
_xhrProgress: function(evt){ |
| 150 |
if(evt.lengthComputable){ |
| 151 |
var o = { |
| 152 |
bytesLoaded:evt.loaded, |
| 153 |
bytesTotal:evt.total, |
| 154 |
type:evt.type, |
| 155 |
timeStamp:evt.timeStamp |
| 156 |
}; |
| 157 |
if(evt.type == "load"){ |
| 158 |
|
| 159 |
o.percent = "100%"; |
| 160 |
o.decimal = 1; |
| 161 |
}else{ |
| 162 |
o.decimal = evt.loaded / evt.total; |
| 163 |
o.percent = Math.ceil((evt.loaded / evt.total)*100)+"%"; |
| 164 |
} |
| 165 |
this.onProgress(o); |
| 166 |
} |
| 167 |
} |
| 168 |
}); |
| 169 |
|
| 170 |
}); |