|
65 | 65 | // Start uploading
|
66 | 66 | options.upload(options, _this);
|
67 | 67 | _this._send.call(_this, options, data);
|
68 |
| - }); |
| 68 | + }, options); |
69 | 69 | },
|
70 | 70 |
|
71 | 71 | _send: function (options, data){
|
|
77 | 77 | url += (~url.indexOf('?') ? '&' : '?') + api.uid();
|
78 | 78 |
|
79 | 79 | if( data.nodeName ){
|
| 80 | + // legacy |
80 | 81 | options.upload(options, _this);
|
81 | 82 |
|
82 | 83 | xhr = document.createElement('div');
|
|
125 | 126 | form = null;
|
126 | 127 | }
|
127 | 128 | else {
|
| 129 | + // html5 |
128 | 130 | xhr = _this.xhr = api.getXHR();
|
129 | 131 |
|
130 | 132 | xhr.open('POST', url, true);
|
|
138 | 140 | xhr.setRequestHeader(key, val);
|
139 | 141 | });
|
140 | 142 |
|
141 |
| - if( xhr.upload ){ |
142 |
| - // https://github.com/blueimp/jQuery-File-Upload/wiki/Fixing-Safari-hanging-on-very-high-speed-connections-%281Gbps%29 |
143 |
| - xhr.upload.addEventListener('progress', api.throttle(function (/**Event*/evt){ |
144 |
| - options.progress(evt, _this, options); |
145 |
| - }, 100), false); |
146 |
| - } |
147 |
| - |
148 |
| - xhr.onreadystatechange = function (){ |
149 |
| - _this.status = xhr.status; |
150 |
| - _this.statusText = xhr.statusText; |
151 |
| - _this.readyState = xhr.readyState; |
| 143 | + |
| 144 | + if (api.support.chunked && options.chunkSize > 0) { |
| 145 | + // resumable upload |
| 146 | + if( xhr.upload ){ |
| 147 | + // https://github.com/blueimp/jQuery-File-Upload/wiki/Fixing-Safari-hanging-on-very-high-speed-connections-%281Gbps%29 |
| 148 | + xhr.upload.addEventListener('progress', api.throttle(function (/**Event*/evt){ |
| 149 | + var e = api.extend({}, evt, { |
| 150 | + loaded : data.start + evt.loaded, |
| 151 | + totalSize : data.size, |
| 152 | + total : data.size}); |
| 153 | + options.progress(e, _this, options); |
| 154 | + }, 100), false); |
| 155 | + } |
152 | 156 |
|
153 |
| - if( xhr.readyState == 4 ){ |
154 |
| - for( var k in { '': 1, XML: 1, Text: 1, Body: 1 } ){ |
155 |
| - _this['response'+k] = xhr['response'+k]; |
| 157 | + xhr.onreadystatechange = function (){ |
| 158 | + _this.status = xhr.status; |
| 159 | + _this.statusText = xhr.statusText; |
| 160 | + _this.readyState = xhr.readyState; |
| 161 | + |
| 162 | + if( xhr.readyState == 4 ){ |
| 163 | + for( var k in { '': 1, XML: 1, Text: 1, Body: 1 } ){ |
| 164 | + _this['response'+k] = xhr['response'+k]; |
| 165 | + } |
| 166 | + xhr.onreadystatechange = null; |
| 167 | + |
| 168 | + if (xhr.status - 201 > 0) { |
| 169 | + // some kind of error |
| 170 | + if (++data.retry <= options.chunkUploadRetry && (500 == xhr.status || 416 == xhr.status)) { |
| 171 | + // let's try again the same chunk |
| 172 | + // only applicable for recoverable error codes 500 && 416 |
| 173 | + data.end = data.start |
| 174 | + _this._send(options, data); |
| 175 | + } else { |
| 176 | + // no mo retries |
| 177 | + _this.end(xhr.status); |
| 178 | + } |
| 179 | + } else { |
| 180 | + // success |
| 181 | + data.retry = 0; |
| 182 | + |
| 183 | + if (data.end == data.size - 1) { |
| 184 | + // finished |
| 185 | + _this.end(xhr.status); |
| 186 | + } else { |
| 187 | + // next chunk |
| 188 | + _this._send(options, data); |
| 189 | + } |
| 190 | + } |
| 191 | + xhr = null; |
156 | 192 | }
|
157 |
| - xhr.onreadystatechange = null; |
158 |
| - _this.end(xhr.status); |
159 |
| - xhr = null; |
| 193 | + }; |
| 194 | + |
| 195 | + data.start = data.end; |
| 196 | + data.end = Math.min(data.end + options.chunkSize, data.size ) - 1; |
| 197 | + |
| 198 | + var slice; |
| 199 | + (slice = 'slice') in data.file || (slice = 'mozSlice') in data.file || (slice = 'webkitSlice') in data.file; |
| 200 | + |
| 201 | + xhr.setRequestHeader("Content-Range", "bytes " + data.start + "-" + data.end + "/" + data.size); |
| 202 | + xhr.setRequestHeader("Content-Disposition", 'attachment; filename=' + data.name); |
| 203 | + |
| 204 | + slice = data.file[slice](data.start, data.end + 1); |
| 205 | + |
| 206 | + xhr.send(slice); |
| 207 | + slice = null; |
| 208 | + } else { |
| 209 | + // single piece upload |
| 210 | + if( xhr.upload ){ |
| 211 | + // https://github.com/blueimp/jQuery-File-Upload/wiki/Fixing-Safari-hanging-on-very-high-speed-connections-%281Gbps%29 |
| 212 | + xhr.upload.addEventListener('progress', api.throttle(function (/**Event*/evt){ |
| 213 | + options.progress(evt, _this, options); |
| 214 | + }, 100), false); |
160 | 215 | }
|
161 |
| - }; |
| 216 | + |
| 217 | + xhr.onreadystatechange = function (){ |
| 218 | + _this.status = xhr.status; |
| 219 | + _this.statusText = xhr.statusText; |
| 220 | + _this.readyState = xhr.readyState; |
| 221 | + |
| 222 | + if( xhr.readyState == 4 ){ |
| 223 | + for( var k in { '': 1, XML: 1, Text: 1, Body: 1 } ){ |
| 224 | + _this['response'+k] = xhr['response'+k]; |
| 225 | + } |
| 226 | + xhr.onreadystatechange = null; |
| 227 | + _this.end(xhr.status); |
| 228 | + xhr = null; |
| 229 | + } |
| 230 | + }; |
162 | 231 |
|
163 |
| - if( api.isArray(data) ){ |
164 |
| - // multipart |
165 |
| - xhr.setRequestHeader('Content-Type', 'multipart/form-data; boundary=_'+api.expando); |
166 |
| - data = data.join('') +'--_'+ api.expando +'--'; |
| 232 | + if( api.isArray(data) ){ |
| 233 | + // multipart |
| 234 | + xhr.setRequestHeader('Content-Type', 'multipart/form-data; boundary=_'+api.expando); |
| 235 | + data = data.join('') +'--_'+ api.expando +'--'; |
167 | 236 |
|
168 |
| - /** @namespace xhr.sendAsBinary https://developer.mozilla.org/ru/XMLHttpRequest#Sending_binary_content */ |
169 |
| - if( xhr.sendAsBinary ){ |
170 |
| - xhr.sendAsBinary(data); |
171 |
| - } |
172 |
| - else { |
173 |
| - var bytes = Array.prototype.map.call(data, function(c){ return c.charCodeAt(0) & 0xff; }); |
174 |
| - xhr.send(new Uint8Array(bytes).buffer); |
| 237 | + /** @namespace xhr.sendAsBinary https://developer.mozilla.org/ru/XMLHttpRequest#Sending_binary_content */ |
| 238 | + if( xhr.sendAsBinary ){ |
| 239 | + xhr.sendAsBinary(data); |
| 240 | + } |
| 241 | + else { |
| 242 | + var bytes = Array.prototype.map.call(data, function(c){ return c.charCodeAt(0) & 0xff; }); |
| 243 | + xhr.send(new Uint8Array(bytes).buffer); |
175 | 244 |
|
| 245 | + } |
| 246 | + } else { |
| 247 | + // FormData |
| 248 | + xhr.send(data); |
176 | 249 | }
|
177 | 250 | }
|
178 |
| - else { |
179 |
| - xhr.send(data); |
180 |
| - } |
181 | 251 | }
|
182 | 252 | }
|
183 | 253 | };
|
|
0 commit comments