Created
January 17, 2017 18:49
-
-
Save voischev/076a9b2514f38714ae99e17e7d701469 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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