Skip to content

Instantly share code, notes, and snippets.

@sajed-zarrinpour
Forked from abr4xas/summernote-upload.js
Last active August 6, 2019 15:01
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 sajed-zarrinpour/9b4f6b8da4e090018ed44ca4ca613f74 to your computer and use it in GitHub Desktop.
Save sajed-zarrinpour/9b4f6b8da4e090018ed44ca4ca613f74 to your computer and use it in GitHub Desktop.
Summernote image upload
$('.summer').summernote({
height: "200px",
callbacks: {
onImageUpload: function(files) {
url = $(this).data('upload'); //path is defined as data attribute for textarea
sendFile(files[0], url, $(this));
}
}
});
function sendFile(file, url, editor) {
var data = new FormData();
data.append("file", file);
var request = new XMLHttpRequest();
//for using it with laravel 5.8 , you need x-csrf-token header
const csrfToken = document.querySelector('input[name="_token"]').value;
request.setRequestHeader('x-csrf-token', csrfToken);
request.open('POST', url, true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
// Success!
var resp = request.responseText;
editor.summernote('insertImage', resp);
console.log(resp);
} else {
// We reached our target server, but it returned an error
var resp = request.responseText;
console.log(resp);
}
};
request.onerror = function(jqXHR, textStatus, errorThrown) {
// There was a connection error of some sort
console.log(jqXHR);
};
request.send(data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment