Skip to content

Commit bdbd385

Browse files
authored
Improve file upload progress (#1029)
* Improve file upload progress https://developers.weixin.qq.com/miniprogram/en/dev/api/network/download/DownloadTask.onProgressUpdate.html https://developer.mozilla.org/en-US/docs/Web/API/ProgressEvent * Add support for wechat * Add length of data downloaded * Add length of total data * improve coverage * fix wechat lengthComputable
1 parent 272c4a8 commit bdbd385

File tree

4 files changed

+51
-33
lines changed

4 files changed

+51
-33
lines changed

src/RESTController.js

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -167,28 +167,18 @@ const RESTController = {
167167
for (const key in customHeaders) {
168168
headers[key] = customHeaders[key];
169169
}
170-
if(options && typeof options.progress === 'function') {
171-
if (xhr.upload) {
172-
xhr.upload.addEventListener('progress', (oEvent) => {
173-
if (oEvent.lengthComputable) {
174-
options.progress(oEvent.loaded / oEvent.total);
175-
} else {
176-
options.progress(null);
177-
}
178-
});
179-
} else if (xhr.addEventListener) {
180-
xhr.addEventListener('progress', (oEvent) => {
181-
if (oEvent.lengthComputable) {
182-
options.progress(oEvent.loaded / oEvent.total);
183-
} else {
184-
options.progress(null);
185-
}
186-
});
187-
}
188-
}
189170
xhr.onabort = () => {
190171
aborted = true;
191172
};
173+
xhr.onprogress = (event) => {
174+
if(options && typeof options.progress === 'function') {
175+
if (event.lengthComputable) {
176+
options.progress(event.loaded / event.total, event.loaded, event.total);
177+
} else {
178+
options.progress(null);
179+
}
180+
}
181+
};
192182
xhr.open(method, url, true);
193183

194184
for (const h in headers) {

src/Xhr.weapp.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports = class XhrWeapp {
1616
this.method = '';
1717
this.url = '';
1818
this.onabort = () => {};
19+
this.onprogress = () => {};
1920
this.onerror = () => {};
2021
this.onreadystatechange = () => {};
2122
this.requestTask = null;
@@ -72,6 +73,14 @@ module.exports = class XhrWeapp {
7273
this.requestTask = null;
7374
this.onerror(err);
7475
}
75-
})
76+
});
77+
this.requestTask.onProgressUpdate((res) => {
78+
const event = {
79+
lengthComputable: (res.totalBytesExpectedToWrite !== 0),
80+
loaded: res.totalBytesWritten,
81+
total: res.totalBytesExpectedToWrite,
82+
};
83+
this.onprogress(event);
84+
});
7685
}
7786
};

src/__tests__/RESTController-test.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -429,15 +429,11 @@ describe('RESTController', () => {
429429
});
430430

431431
it('reports upload progress of the AJAX request when callback is provided', (done) => {
432-
const xhr = mockXHR([{ status: 200, response: { success: true }}], {
433-
addEventListener: (name, callback) => {
434-
if(name === "progress") {
435-
callback({
436-
lengthComputable: true,
437-
loaded: 5,
438-
total: 10
439-
});
440-
}
432+
const xhr = mockXHR([{ status: 200, response: { success: true } }], {
433+
progress: {
434+
lengthComputable: true,
435+
loaded: 5,
436+
total: 10
441437
}
442438
});
443439
RESTController._setXHR(xhr);
@@ -448,7 +444,30 @@ describe('RESTController', () => {
448444
jest.spyOn(options, 'progress');
449445

450446
RESTController.ajax('POST', 'files/upload.txt', {}, {}, options).then(({ response, status }) => {
451-
expect(options.progress).toHaveBeenCalledWith(0.5);
447+
expect(options.progress).toHaveBeenCalledWith(0.5, 5, 10);
448+
expect(response).toEqual({ success: true });
449+
expect(status).toBe(200);
450+
done();
451+
});
452+
});
453+
454+
it('does not upload progress when total is uncomputable', (done) => {
455+
const xhr = mockXHR([{ status: 200, response: { success: true } }], {
456+
progress: {
457+
lengthComputable: false,
458+
loaded: 5,
459+
total: 0
460+
}
461+
});
462+
RESTController._setXHR(xhr);
463+
464+
const options = {
465+
progress: function(){}
466+
};
467+
jest.spyOn(options, 'progress');
468+
469+
RESTController.ajax('POST', 'files/upload.txt', {}, {}, options).then(({ response, status }) => {
470+
expect(options.progress).toHaveBeenCalledWith(null);
452471
expect(response).toEqual({ success: true });
453472
expect(status).toBe(200);
454473
done();

src/__tests__/test_helpers/mockXHR.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* alongside it.
1717
* `upload` can be provided to mock the XMLHttpRequest.upload property.
1818
*/
19-
function mockXHR(results, upload) {
19+
function mockXHR(results, options = {}) {
2020
const XHR = function() { };
2121
let attempts = 0;
2222
XHR.prototype = {
@@ -28,8 +28,8 @@ function mockXHR(results, upload) {
2828
this.readyState = 4;
2929
attempts++;
3030
this.onreadystatechange();
31-
},
32-
upload: upload
31+
this.onprogress(options.progress);
32+
}
3333
};
3434
return XHR;
3535
}

0 commit comments

Comments
 (0)