Skip to content

Instantly share code, notes, and snippets.

@voischev
Created January 17, 2017 18:49
Show Gist options
  • Save voischev/076a9b2514f38714ae99e17e7d701469 to your computer and use it in GitHub Desktop.
Save voischev/076a9b2514f38714ae99e17e7d701469 to your computer and use it in GitHub Desktop.
$(function() {
var showInfo = function(message) {
$('div.progress').hide();
$('strong.message').text(message);
$('div.alert').show();
};
$('input[type="submit"]').on('click', function(evt) {
evt.preventDefault();
$('div.progress').show();
var formData = new FormData();
var file = document.getElementById('myFile').files[0];
formData.append('myFile', file);
var xhr = new XMLHttpRequest();
xhr.open('post', '/', true);
xhr.upload.onprogress = function(e) {
if (e.lengthComputable) {
var percentage = (e.loaded / e.total) * 100;
$('div.progress div.bar').css('width', percentage + '%');
}
};
xhr.onerror = function(e) {
showInfo('An error occurred while submitting the form. Maybe your file is too big');
};
xhr.onload = function() {
showInfo(this.statusText);
};
xhr.send(formData);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment