Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split progress event handlers into upload and download. #423

Merged
merged 7 commits into from
Aug 23, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Updating progress handling to work regardless of method, split upload…
… and download handlers
  • Loading branch information
Dylan Lundy committed Aug 22, 2016
commit 79abdb5d0ed675fdfe6a669af106e7c4da8946f5
2 changes: 1 addition & 1 deletion examples/upload/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ <h1>file upload</h1>
data.append('file', document.getElementById('file').files[0]);

var config = {
progress: function(progressEvent) {
progressUpload: function(progressEvent) {
var percentCompleted = progressEvent.loaded / progressEvent.total;
}
};
Expand Down
13 changes: 7 additions & 6 deletions lib/adapters/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,15 @@ module.exports = function xhrAdapter(config) {
}

// Handle progress if needed
if (typeof config.progress === 'function') {
if (config.method === 'post' || config.method === 'put') {
request.upload.addEventListener('progress', config.progress);
} else if (config.method === 'get') {
request.addEventListener('progress', config.progress);
}
if (typeof config.progressDownload === 'function') {
request.addEventListener('progress', config.progressDownload);
}

if (typeof config.progressUpload === 'function') {
request.upload.addEventListener('progress', config.progressUpload);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like upload is supported by all modern browsers (please correct me if I'm wrong) so I would suggest to check whether this property is present before calling addEventListener:

if (typeof config.progressUpload === 'function' && request.upload) {
  // ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not really clear which browsers support request.upload and when they did. It was included in the XHR2 spec so older browsers definitely won't support it. Will add a check.

}


if (requestData === undefined) {
requestData = null;
}
Expand Down