Skip to content

Instantly share code, notes, and snippets.

@taufik-nurrohman
Last active December 9, 2021 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taufik-nurrohman/430255d407912a8f44c94f7ae852e8e9 to your computer and use it in GitHub Desktop.
Save taufik-nurrohman/430255d407912a8f44c94f7ae852e8e9 to your computer and use it in GitHub Desktop.
$(function() {
function showToast(parent, text, timeOut) {
let toast = $('<div></div>').html(text);
toast.appendTo(parent);
window.setTimeout(function() {
toast.remove();
}, timeOut);
}
const input = $(':file');
input.change(function() {
let t = $(this),
data = new FormData(),
json, text = "",
xhr = new XMLHttpRequest();
data.append('imageToUpload', this.files[0]);
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
json = JSON.parse(this.responseText);
text += '<div class="alert alert-info mt-4">';
text += json.messages + '<br>';
text += json.details.type + '<br>';
text += json.details.size + '<br>';
text += json.details.path.replace(/^(Path:\s*)(.*?)$/i, '$1<a href="$2" target="_blank">$2</a>');
text += '</div>';
// Display response text after the uploaded file
showToast(t.parent(), text, 3000);
}
};
xhr.open('POST', '/stockx/dashboard/uploadImage', true);
xhr.send(data);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment