summernote 粘贴图片上传到服务器
以下为关键代码
//初始化富文本
$('#description').summernote('destroy');
$('#description').summernote({
width:'100%',
height:250,
lang: 'zh-CN',
focus: true,
toolbar: [
['style', ['bold', 'italic', 'underline', 'clear']],
['fontsize', ['fontsize']],
['color', ['color']],
['para', ['ul', 'ol', 'paragraph']],
['height', ['height']],
['insert', ['picture']]
],
callbacks: {
onImageUpload:function(files){
sendFile(files,'#description');
},
onPaste: function(event) {
console.log(event);
if ( event.clipboardData || event.originalEvent ) {
var bufferText = ((event.originalEvent || event).clipboardData || window.clipboardData).getData('Text/plain');
if(bufferText.length>0)
{
;
}else
event.preventDefault();
}
}
}
});
文件上传细节:
function sendFile(files, editor) {
var formData = new FormData();
formData.append('file',files[0]);
$.ajax({
url : '/file/upload',//后台文件上传接口
type : 'POST',
data : formData,
processData : false,
contentType : false,
success : function(res) {
if(res.result==1)
{
var path='/file/download'+"?fileId="+res.detail;//后台展示图片地址
$(editor).summernote('insertImage', path, function ($image) {
$image.css('width', 'auto');
});
}else
layer.msg(res.message, {shift: 5});
},error:function(){
alert("上传失败");
}
});
}
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/243635.html