经过前面两篇文章的铺垫,接下来就是重点了,最原始的summernote的图片上传功能还未完善,现在我们要做的就是修改这个功能,使其功能完善派上用场。
summernote提供了一些方法的重写,我们就来重写其上传图片的方法onImageUpload,然后将data数据传送到upload.do这个controller,data数据中包含了上传图片的名字。editor.insertImage()的作用是在编辑器中显示已经上传的图片,第一个参数表示当前编辑器,第二个参数是图片保存的路径。
<script>
$(document).ready(function() {
$('#summernote').summernote({
height:300,
focus:true,
maxHeight:null,
minHeight:null,
onImageUpload: function(files, editor, welEditable) {
sendFile(files[0],editor,welEditable);
}
});
});
function sendFile(files,editor,welEditable) {
data = new FormData();
data.append("file", files);
$.ajax({
data: data,
type: "POST",
url: "/lol/upload.do",
cache: false,
contentType: false,
processData: false,
success: function(data) {