Skip to content

Commit

Permalink
fixed upload progress
Browse files Browse the repository at this point in the history
  • Loading branch information
ShivamJoker committed Aug 19, 2020
1 parent ffb9a0f commit ad7a70a
Showing 1 changed file with 16 additions and 28 deletions.
44 changes: 16 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,23 @@ const uploadFile = () => {
// upload file
const xhr = new XMLHttpRequest();

// xhr.upload.onprogress = updateProgress;
xhr.addEventListener("load", transferComplete);
xhr.addEventListener("error", transferFailed);
xhr.addEventListener("abort", transferCanceled);
// listen for upload progress
xhr.upload.onprogress = function (event) {
let percent = Math.round((100 * event.loaded) / event.total);
console.log(`File is ${percent} uploaded.`);
};

// handle error
xhr.upload.onerror = function () {
console.log(`Error during the upload: ${xhr.status}.`);
};

// upload completed successfully
xhr.onload = function () {
console.log("Upload completed successfully.");
};

console.log(xhr);
// listen for response which will give the link
xhr.onreadystatechange = function () {
if (xhr.readyState == XMLHttpRequest.DONE) {
console.log(xhr.responseText);
Expand All @@ -57,26 +68,3 @@ const uploadFile = () => {
xhr.open("POST", "http:https://localhost:3000/api/files");
xhr.send(formData);
};

// progress on transfers from the server to the client (downloads)
function updateProgress(oEvent) {
if (oEvent.lengthComputable) {
var percentComplete = (oEvent.loaded / oEvent.total) * 100;
console.log(percentComplete);
// ...
} else {
// Unable to compute progress information since the total size is unknown
}
}

function transferComplete(evt) {
console.log("The transfer is complete.", evt);
}

function transferFailed(evt) {
console.log("An error occurred while transferring the file.");
}

function transferCanceled(evt) {
console.log("The transfer has been canceled by the user.");
}

0 comments on commit ad7a70a

Please sign in to comment.