Skip to content

Instantly share code, notes, and snippets.

@DmitrySikorsky
Created February 1, 2017 16:41
Show Gist options
  • Save DmitrySikorsky/c810cad8ae72e5d4647646f51298defe to your computer and use it in GitHub Desktop.
Save DmitrySikorsky/c810cad8ae72e5d4647646f51298defe to your computer and use it in GitHub Desktop.
function uploadFiles(inputId) {
var input = document.getElementById(inputId);
var files = input.files;
var formData = new FormData();
for (var i = 0; i != files.length; i++) {
formData.append("files", files[i]);
}
startUpdatingProgressIndicator();
$.ajax(
{
url: "/uploader",
data: formData,
processData: false,
contentType: false,
type: "POST",
success: function (data) {
stopUpdatingProgressIndicator();
alert("Files Uploaded!");
}
}
);
}
var intervalId;
function startUpdatingProgressIndicator() {
$("#progress").show();
intervalId = setInterval(
function () {
// We use the POST requests here to avoid caching problems (we could use the GET requests and disable the cache instead)
$.post(
"/uploader/progress",
function (progress) {
$("#bar").css({ width: progress + "%" });
$("#label").html(progress + "%");
}
);
},
10
);
}
function stopUpdatingProgressIndicator() {
clearInterval(intervalId);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment