Skip to content

Instantly share code, notes, and snippets.

@jovanialferez
Created March 9, 2020 11:23
Show Gist options
  • Save jovanialferez/89d5d91985b5b52a582ae8205dee3efb to your computer and use it in GitHub Desktop.
Save jovanialferez/89d5d91985b5b52a582ae8205dee3efb to your computer and use it in GitHub Desktop.
// #js-profile-pic-upload --> <input id='js-profile-pic-upload' type=file />
$('#js-profile-pic-upload').on('change', function () {
const supplierId = $(this).data('supplierId');
var form = new FormData();
form.append('photo', $(this)[0].files[0]);
$.ajax({
type: 'POST',
url: `/upload-photo`,
data: form,
cache: false,
contentType: false,
processData: false,
success: resp => $('#js-profile-pic').attr('src', resp.url),
error: err => console.error(err)
});
// preview while uploading above is in progress
var reader = new FileReader();
reader.onload = (e) => {
// That image dom element to show the "preview"
$('#js-profile-pic').attr('src', e.target.result);
}
reader.readAsDataURL($(this)[0].files[0]);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment