Skip to content

Instantly share code, notes, and snippets.

@sujeetkv
Last active October 11, 2018 07:45
Show Gist options
  • Save sujeetkv/66cc02aeead1bd19b3763f4456245c82 to your computer and use it in GitHub Desktop.
Save sujeetkv/66cc02aeead1bd19b3763f4456245c82 to your computer and use it in GitHub Desktop.
File upload progress
$('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