Skip to content

Instantly share code, notes, and snippets.

@willyarisky
Created July 30, 2019 15:56
Show Gist options
  • Save willyarisky/3dca180bba11663cd8d3f4e93b798bee to your computer and use it in GitHub Desktop.
Save willyarisky/3dca180bba11663cd8d3f4e93b798bee to your computer and use it in GitHub Desktop.
jQuery ajax upload with progress bar
$('#photo-uploader').change((e) => {
e.preventDefault();
$.ajax({
url: '/photo/upload',
type: 'POST',
processData: false,
contentType: false,
data: new FormData($('#print-order-form')[0]),
beforeSend: () => {
$('.progress').show();
},
xhr: () => {
var xhr = $.ajaxSettings.xhr();
xhr.upload.onprogress = (e) => {
var percentProgress = Math.floor(e.loaded / e.total *100) + '%';
$('.progress-bar').css('width', percentProgress);
$('.progress-bar').text(percentProgress);
};
return xhr;
},
success: (res) => {
$('.progress').hide();
$('.progress-bar').css('width', '0%');
$('.progress-bar').text('0%');
console.log(res);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment