Last active
October 11, 2018 07:45
-
-
Save sujeetkv/66cc02aeead1bd19b3763f4456245c82 to your computer and use it in GitHub Desktop.
File upload progress
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
$('form').on('submit', function (evt) { | |
evt.preventDefault(); | |
var form = this; | |
var $form = $(this); | |
$.ajax({ | |
url: $form.attr('action'), | |
type: 'post', | |
data: new FormData(form), | |
cache: false, | |
processData: false, | |
contentType: false, | |
xhr: function () { | |
var myXhr = $.ajaxSettings.xhr(); | |
if (myXhr.upload && myXhr.upload.addEventListener) { | |
/* Events: loadstart, progress, abort, error, load, timeout, loadend */ | |
myXhr.upload.addEventListener('progress', function (event) { | |
// upload progress | |
if (event.lengthComputable) { | |
var percentComplete = Math.round(event.loaded / event.total * 100); | |
$form.find('#prograss-bar').val(percentComplete); | |
} | |
}, false); | |
} | |
return myXhr; | |
}, | |
beforeSend: function (xhr, settings) {}, | |
success: function (data, status, xhr) {}, | |
error: function (xhr, status, err) {}, | |
complete: function (xhr, status) {}, | |
}); | |
return false; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment