<script type="text/javascript" src="${ctx }/static/js/wangEditor.min.js"></script>
var E = window.wangEditor
var editor = new E('#editor')
// 配置服务器端地址
editor.customConfig.uploadImgServer = '/upload'
editor.customConfig.withCredentials = true
editor.customConfig.uploadFileName = 'files'
editor.customConfig.uploadImgHeaders = {
}
editor.customConfig.uploadImgParams = {
}
editor.customConfig.uploadImgHooks = {
before: function (xhr, editor, files) {
console.info(editor);
},
success: function (xhr, editor, result) {
// 图片上传并返回结果,图片插入成功之后触发
// xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,result 是服务器端返回的结果
},
fail: function (xhr, editor, result) {
},
error: function (xhr, editor) {
},
timeout: function (xhr, editor) {
},
customInsert: function (insertImg, result, editor) {
var url = result
insertImg(url)
}
}
editor.create()
editor.txt.html('${article.content}')
html代码
<div id="editor">
</div>
得到富文本内容
editor.txt.html()
图片上传代码
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public String uploadFile(MultipartHttpServletRequest multipartRequest,HttpServletResponse response) throws Exception {
String fileId = "";
String extName = "";
// 获得文件:
MultipartFile file = multipartRequest.getFile("files");
// 获得文件名:
String filename = file.getOriginalFilename();
if (filename.endsWith(".jsp") || filename.endsWith(".php") || filename.endsWith(".asp") || filename.endsWith(".aspx") || filename.endsWith(".js")) {
return "error";
} else {
// 获得输入流:
InputStream input = file.getInputStream();
extName = filename.substring(filename.lastIndexOf(".") + 1);
fileId = this.operationService.storeFile(input, extName);
}
response.setHeader("Access-Control-Allow-Origin", "*");
return fileId + "." + extName;
}
private static final String ORIGINAL_PATH = "ori";
public String storeFile(InputStream inputStream, String extName) throws Exception {
String picId = DateUtil.getDateString(new Date(), "yyyyMMddHHmmss") + ObjectId.get();
String filePath = "";
if (ImageUtil.isImage(extName)) {
filePath = imageDir + "/" + ORIGINAL_PATH + "/" + picId + "." + extName;
} else {
filePath = fileDir + "/" + picId + "." + extName;
}
FileUtil.InputStreamTOFile(inputStream, filePath);
return picId;
}