Skip to content

Commit f735df0

Browse files
committed
Merge pull request #140 from shtyftu/odklFixes
Some fixes for XHR.
2 parents 129b396 + e1bbdbd commit f735df0

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

lib/FileAPI.XHR.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@
158158
if ( options._chunked ) {
159159
// chunked upload
160160
if( xhr.upload ){
161-
xhr.upload.addEventListener('progress', function (/**Event*/evt){
161+
xhr.upload.addEventListener('progress', api.throttle(function (/**Event*/evt){
162162
if (!data.retry) {
163163
// show progress only for correct chunk uploads
164164
options.progress({
@@ -168,7 +168,7 @@
168168
, totalSize: data.size
169169
}, _this, options);
170170
}
171-
}, false);
171+
}, 100), false);
172172
}
173173

174174
xhr.onreadystatechange = function (){
@@ -204,6 +204,9 @@
204204
data.end = lkb;
205205
} else {
206206
data.end = data.start - 1;
207+
if (416 == xhr.status) {
208+
data.end = data.end - options.chunkSize;
209+
}
207210
}
208211

209212
setTimeout(function () {
@@ -237,6 +240,9 @@
237240
}
238241
}
239242
xhr = null;
243+
if (slice) {
244+
slice = null;
245+
}
240246
}
241247
};
242248

@@ -246,13 +252,20 @@
246252
var slice;
247253
(slice = 'slice') in data.file || (slice = 'mozSlice') in data.file || (slice = 'webkitSlice') in data.file;
248254

249-
xhr.setRequestHeader("Content-Range", "bytes " + data.start + "-" + data.end + "/" + data.size);
250-
xhr.setRequestHeader("Content-Disposition", 'attachment; filename=' + encodeURIComponent(data.name));
251-
xhr.setRequestHeader("Content-Type", data.type || "application/octet-stream");
252-
253-
slice = data.file[slice](data.start, data.end + 1);
254-
255-
xhr.send(slice);
255+
slice = data.file[slice](data.start, data.end + 1);
256+
257+
if (data.size && !slice.size) {
258+
setTimeout(function () {
259+
_this.end(-1);
260+
})
261+
} else {
262+
xhr.setRequestHeader("Content-Range", "bytes " + data.start + "-" + data.end + "/" + data.size);
263+
xhr.setRequestHeader("Content-Disposition", 'attachment; filename=' + encodeURIComponent(data.name));
264+
xhr.setRequestHeader("Content-Type", data.type || "application/octet-stream");
265+
266+
xhr.send(slice);
267+
}
268+
256269
slice = null;
257270
} else {
258271
// single piece upload

0 commit comments

Comments
 (0)