1. <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
  2. <script src="https://unpkg.com/wangeditor/dist/wangEditor.min.js"></script>
  1. <div id="editor"></div>
  2. <textarea style="display: none" name="blogContent" id="blogContent"></textarea>
  1. <!-- wangEditor 富文本编辑器 js -->
  2. <script type="text/javascript">
  3. var E = window.wangEditor;
  4. var editor = new E('#editor');
  5. // 获取wangEditor中的内容
  6. var blogContent = $('#blogContent');
  7. // 监控wangEditor中的内容变化,并将html内容同步更新到 textarea
  8. editor.config.onchange = function (newHtml) {
  9. blogContent.val(newHtml);
  10. };
  11. editor.config.uploadImgServer = '[[@{/user/upload}]]'; // 文件上传接口
  12. editor.config.uploadImgMaxSize = 5 * 1024 * 1024; // 文件大小5M
  13. editor.config.uploadImgMaxLength = 9;
  14. editor.config.uploadFileName = 'files';
  15. editor.config.uploadImgHooks = {
  16. success: function (xhr, editor, result) {
  17. console.log("上传成功");
  18. },
  19. fail: function (xhr, editor, result) {
  20. console.log("上传失败,原因是" + result);
  21. },
  22. error: function (xhr, editor) {
  23. console.log("上传出错");
  24. },
  25. customInsert: function(insertImgFn, result) {
  26. // 图片上传并返回结果
  27. var url = result.url;
  28. insertImgFn(url);
  29. }
  30. }
  31. editor.create();
  32. // 初始化 textarea 的值
  33. blogContent.val(editor.txt.html());
  34. </script>
  1. // 上传文件
  2. @ResponseBody
  3. @RequestMapping("/upload")
  4. public String fileUpload(@RequestParam("files") MultipartFile files) throws IOException {
  5. // // win系统 上传路径保存设置
  6. // // 获取项目路径
  7. // File projectPath = new File(ResourceUtils.getURL("classpath:").getPath());
  8. // // 绝对路径=项目路径+自定义路径
  9. // File pathFile = new File(projectPath.getAbsolutePath(), "static/upload/");
  10. // if (!pathFile.exists()) {
  11. // pathFile.mkdirs();
  12. // }
  13. // //上传文件地址
  14. // UUID uuid = UUID.randomUUID();
  15. // File serverFile = new File(pathFile, uuid + "_" + files.getOriginalFilename());
  16. // files.transferTo(serverFile);
  17. //
  18. // String imgPath = ("/upload/" + uuid + "_" + files.getOriginalFilename()).replace("\\", "/");
  19. //
  20. // return imgPath;
  21. // Linux服务器 上传路径保存设置
  22. // 项目路径 /home/www/
  23. File pathFile = new File("/home/www/upload/");
  24. if (!pathFile.exists()) {
  25. pathFile.mkdirs();
  26. }
  27. //上传文件地址
  28. UUID uuid = UUID.randomUUID();
  29. File serverFile = new File(pathFile, uuid + "_" + files.getOriginalFilename());
  30. files.transferTo(serverFile);
  31. String imgPath = ("/upload/" + uuid + "_" + files.getOriginalFilename()).replace("\\", "/");
  32. return imgPath;
  33. }
  1. <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
  2. <script src="https://unpkg.com/wangeditor/dist/wangEditor.min.js"></script>

 

  1. <!-- ${urDetailBlog.getBlogContent()} 后台获取的WangEditor编辑器提交的html代码 -->
  2. <div id="detailBlog1" hidden th:text="${urDetailBlog.getBlogContent()}"></div>
  3. <div id="detailBlog"></div>
  1. <script>
  2. $(function() {
  3. $("#detailBlog").html($("#detailBlog1").text());
  4. });
  5. </script>

 

 

 

版权声明:本文为dmflysky原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/dmflysky/p/14451376.html