-
-
Save sajed-zarrinpour/9b4f6b8da4e090018ed44ca4ca613f74 to your computer and use it in GitHub Desktop.
Summernote image upload
This file contains 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
$('.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