1 | define([ |
---|
2 | "dojo/_base/declare", |
---|
3 | "dojo/_base/lang", |
---|
4 | "dojo/_base/array", |
---|
5 | "dojo" |
---|
6 | ],function(declare, lang, arrayUtil, dojo){ |
---|
7 | |
---|
8 | return declare("dojox.form.uploader._HTML5", [], { |
---|
9 | // summary: |
---|
10 | // A mixin for dojox/form/Uploader that adds HTML5 multiple-file upload capabilities and |
---|
11 | // progress events. |
---|
12 | // |
---|
13 | // description: |
---|
14 | // Note that this does not add these capabilities to browsers that don't support them. |
---|
15 | // For IE8 or older browsers, _IFrame or _Flash mixins will be used. |
---|
16 | // |
---|
17 | |
---|
18 | // debug message: |
---|
19 | errMsg:"Error uploading files. Try checking permissions", |
---|
20 | |
---|
21 | // Overwrites "form" and could possibly be overwritten again by iframe or flash plugin. |
---|
22 | uploadType:"html5", |
---|
23 | |
---|
24 | postMixInProperties: function(){ |
---|
25 | this.inherited(arguments); |
---|
26 | if(this.uploadType === "html5"){ } |
---|
27 | }, |
---|
28 | |
---|
29 | postCreate: function(){ |
---|
30 | this.connectForm(); |
---|
31 | this.inherited(arguments); |
---|
32 | if(this.uploadOnSelect){ |
---|
33 | this.connect(this, "onChange", function(data){ |
---|
34 | this.upload(data[0]); |
---|
35 | }); |
---|
36 | } |
---|
37 | }, |
---|
38 | |
---|
39 | _drop: function(e){ |
---|
40 | dojo.stopEvent(e); |
---|
41 | var dt = e.dataTransfer; |
---|
42 | this._files = dt.files; |
---|
43 | this.onChange(this.getFileList()); |
---|
44 | }, |
---|
45 | /************************* |
---|
46 | * Public Methods * |
---|
47 | *************************/ |
---|
48 | |
---|
49 | upload: function(/*Object ? */ formData){ |
---|
50 | // summary: |
---|
51 | // See: dojox.form.Uploader.upload |
---|
52 | |
---|
53 | this.onBegin(this.getFileList()); |
---|
54 | this.uploadWithFormData(formData); |
---|
55 | }, |
---|
56 | |
---|
57 | addDropTarget: function(node, /*Boolean?*/ onlyConnectDrop){ |
---|
58 | // summary: |
---|
59 | // Add a dom node which will act as the drop target area so user |
---|
60 | // can drop files to this node. |
---|
61 | // description: |
---|
62 | // If onlyConnectDrop is true, dragenter/dragover/dragleave events |
---|
63 | // won't be connected to dojo.stopEvent, and they need to be |
---|
64 | // canceled by user code to allow DnD files to happen. |
---|
65 | // This API is only available in HTML5 plugin (only HTML5 allows |
---|
66 | // DnD files). |
---|
67 | if(!onlyConnectDrop){ |
---|
68 | this.connect(node, 'dragenter', dojo.stopEvent); |
---|
69 | this.connect(node, 'dragover', dojo.stopEvent); |
---|
70 | this.connect(node, 'dragleave', dojo.stopEvent); |
---|
71 | } |
---|
72 | this.connect(node, 'drop', '_drop'); |
---|
73 | }, |
---|
74 | |
---|
75 | uploadWithFormData: function(/*Object*/ data){ |
---|
76 | // summary: |
---|
77 | // Used with WebKit and Firefox 4+ |
---|
78 | // Upload files using the much friendlier FormData browser object. |
---|
79 | // tags: |
---|
80 | // private |
---|
81 | |
---|
82 | if(!this.getUrl()){ |
---|
83 | console.error("No upload url found.", this); return; |
---|
84 | } |
---|
85 | var fd = new FormData(), fieldName=this._getFileFieldName(); |
---|
86 | arrayUtil.forEach(this._files, function(f, i){ |
---|
87 | fd.append(fieldName, f); |
---|
88 | }, this); |
---|
89 | |
---|
90 | if(data){ |
---|
91 | data.uploadType = this.uploadType; |
---|
92 | for(var nm in data){ |
---|
93 | fd.append(nm, data[nm]); |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | var xhr = this.createXhr(); |
---|
98 | xhr.send(fd); |
---|
99 | }, |
---|
100 | |
---|
101 | _xhrProgress: function(evt){ |
---|
102 | if(evt.lengthComputable){ |
---|
103 | var o = { |
---|
104 | bytesLoaded:evt.loaded, |
---|
105 | bytesTotal:evt.total, |
---|
106 | type:evt.type, |
---|
107 | timeStamp:evt.timeStamp |
---|
108 | }; |
---|
109 | if(evt.type == "load"){ |
---|
110 | // 100% |
---|
111 | o.percent = "100%"; |
---|
112 | o.decimal = 1; |
---|
113 | }else{ |
---|
114 | o.decimal = evt.loaded / evt.total; |
---|
115 | o.percent = Math.ceil((evt.loaded / evt.total)*100)+"%"; |
---|
116 | } |
---|
117 | this.onProgress(o); |
---|
118 | } |
---|
119 | }, |
---|
120 | |
---|
121 | createXhr: function(){ |
---|
122 | var xhr = new XMLHttpRequest(); |
---|
123 | var timer; |
---|
124 | xhr.upload.addEventListener("progress", lang.hitch(this, "_xhrProgress"), false); |
---|
125 | xhr.addEventListener("load", lang.hitch(this, "_xhrProgress"), false); |
---|
126 | xhr.addEventListener("error", lang.hitch(this, function(evt){ |
---|
127 | this.onError(evt); |
---|
128 | clearInterval(timer); |
---|
129 | }), false); |
---|
130 | xhr.addEventListener("abort", lang.hitch(this, function(evt){ |
---|
131 | this.onAbort(evt); |
---|
132 | clearInterval(timer); |
---|
133 | }), false); |
---|
134 | xhr.onreadystatechange = lang.hitch(this, function(){ |
---|
135 | if(xhr.readyState === 4){ |
---|
136 | // console.info("COMPLETE") |
---|
137 | clearInterval(timer); |
---|
138 | try{ |
---|
139 | this.onComplete(JSON.parse(xhr.responseText.replace(/^\{\}&&/,''))); |
---|
140 | }catch(e){ |
---|
141 | var msg = "Error parsing server result:"; |
---|
142 | console.error(msg, e); |
---|
143 | console.error(xhr.responseText); |
---|
144 | this.onError(msg, e); |
---|
145 | } |
---|
146 | } |
---|
147 | }); |
---|
148 | xhr.open("POST", this.getUrl()); |
---|
149 | xhr.setRequestHeader("Accept","application/json"); |
---|
150 | |
---|
151 | timer = setInterval(lang.hitch(this, function(){ |
---|
152 | try{ |
---|
153 | if(typeof(xhr.statusText)){} // accessing this error throws an error. Awesomeness. |
---|
154 | }catch(e){ |
---|
155 | //this.onError("Error uploading file."); // not always an error. |
---|
156 | clearInterval(timer); |
---|
157 | } |
---|
158 | }),250); |
---|
159 | |
---|
160 | return xhr; |
---|
161 | } |
---|
162 | |
---|
163 | }); |
---|
164 | |
---|
165 | }); |
---|