Created
July 30, 2019 15:56
-
-
Save willyarisky/3dca180bba11663cd8d3f4e93b798bee to your computer and use it in GitHub Desktop.
jQuery ajax upload with progress bar
This file contains hidden or 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
$('#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