• 项目如何实现富文本框文件上传


    wangEditor作为富文本框

    第一步:安装wangEditor

    1. yarn add @wangeditor/editor
    2. # 或者 npm install @wangeditor/editor --save
    3. yarn add @wangeditor/editor-for-vue
    4. # 或者 npm install @wangeditor/editor-for-vue --save
    5. yarn add @wangeditor/plugin-upload-attachment

    第二:写富文本页面,单独写为一个组件

    1. <script>
    2. import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
    3. import Vue from 'vue'
    4. import { ACCESS_TOKEN } from '@/store/mutation-types'
    5. import attachmentModule from '@wangeditor/plugin-upload-attachment'
    6. import { Boot } from '@wangeditor/editor'
    7. export default {
    8. name: 'MyEditor',
    9. components: { Editor, Toolbar },
    10. data() {
    11. return {
    12. editor: null,
    13. html: '

      hello world

      '
      ,
    14. toolbarConfig: {
    15. insertKeys: {
    16. index: 0, // 自定义插入的位置
    17. keys: ['uploadAttachment'], // “上传附件”菜单
    18. },
    19. // toolbarKeys: [ /* 显示哪些菜单,如何排序、分组 */ ],
    20. excludeKeys: [
    21. 'fullScreen'
    22. ],
    23. },
    24. editorConfig: {
    25. placeholder: '请输入内容...',
    26. // autoFocus: false,
    27. hoverbarKeys: {
    28. attachment: {
    29. menuKeys: ['downloadAttachment'], // “下载附件”菜单
    30. },
    31. },
    32. // 所有的菜单配置,都要在 MENU_CONF 属性下
    33. MENU_CONF: {
    34. uploadImage: {
    35. server:'',
    36. maxFileSize: 1024 * 1024 * 1024,
    37. fieldName: "file",
    38. meta: {
    39. token: '',
    40. },
    41. metaWithUrl: true,
    42. },
    43. uploadVideo: {
    44. server: ,
    45. maxFileSize: 1024 * 1024 * 1024,
    46. fieldName: "file",
    47. meta: {
    48. token:'',
    49. },
    50. metaWithUrl: true,
    51. },
    52. uploadAttachment: {
    53. server: '',
    54. fieldName: "file",
    55. meta: {
    56. token: '',
    57. },
    58. metaWithUrl: true,
    59. maxFileSize: 1024 * 1024 * 1024, // 10M
    60. }
    61. }
    62. }
    63. }
    64. },
    65. props: {
    66. //父附件传递过来的数据,用来保持编辑时数据同步
    67. initData: {
    68. type: String,
    69. require: false,
    70. default: ""
    71. }
    72. },
    73. created() {
    74. if (Boot.plugins.length < 13) {
    75. //判断如果已经插入进去,不在二次插入
    76. Boot.registerModule(attachmentModule);
    77. }
    78. },
    79. methods: {
    80. onCreated(editor) {
    81. this.editor = Object.seal(editor) // 【注意】一定要用 Object.seal() 否则会报错
    82. },
    83. onChange(editor) {
    84. this.$emit("SynchronizationData", editor.getHtml())
    85. },
    86. },
    87. mounted() {
    88. // 模拟 ajax 请求,异步渲染编辑器
    89. setTimeout(() => {
    90. this.html = '

      输入内容

      '
    91. if (this.initData !== "") {
    92. this.html = this.initData
    93. }
    94. }, 1000)
    95. },
    96. beforeDestroy() {
    97. const editor = this.editor
    98. if (editor == null) return
    99. editor.destroy() // 组件销毁时,及时销毁 editor ,重要!!!
    100. },
    101. }
    102. script>
    103. <style src="@wangeditor/editor/dist/css/style.css">style>

    第三步:父组件引用

      <Editor :initData="initData" @SynchronizationData="SynchronizationData">Editor>

     第四步:写后台

    1. @PostMapping(value = "/fileReleasesUpload")
    2. public FileUploadVo fileReleasesUpload(HttpServletRequest request, HttpServletResponse response) {
    3. FileUploadVo vo = new FileUploadVo();
    4. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    5. MultipartFile file = multipartRequest.getFile("file");// 获取上传文件对象
    6. //具体文件上传
    7. CommonUtils.upload(file);
    8. if (上传成功) {
    9. FileUploadVo.FileEsNode esNode = new FileUploadVo.FileEsNode();
    10. esNode.setAlt(file.getOriginalFilename());
    11. esNode.setUrl(prePath+savePath);
    12. vo.setData(esNode);
    13. vo.setErrno(0);
    14. return vo;
    15. } else {
    16. vo.setErrno(1);
    17. vo.setMessage(false);
    18. return vo;
    19. }
    20. }

    返回值需要按照要求格式:

    定义返回类:

    1. import java.io.Serializable;
    2. @Data
    3. public class FileUploadVo implements Serializable {
    4. private Integer errno;
    5. private FileEsNode data;
    6. private Boolean message;
    7. @Data
    8. public static class FileEsNode {
    9. String url;// 视频/图片 src ,必须
    10. String alt;// 图片描述文字,非必须
    11. String href;// 图片的链接,非必须
    12. String poster;//上传视频时返回的封面,非必须
    13. }
    14. }

    上传成功后,上传文件地址嵌入文本框,无需对上传成功后返回值做另外处理,只要保证返回值是规定格式就可。

  • 相关阅读:
    远程导入MySQL数据量大速度慢问题
    向毕业妥协系列之机器学习笔记:无监督学习-异常检测
    Java程序输入、输出、动态数组、列表、类型转换、字符串遍历
    Rust China Hackathon 2022 达坦科技组空中宣讲会来啦!
    BP神经网络参数设置总结
    RHCE学习 --- 第一次作业
    基于ClickHouse解决活动海量数据问题
    国际阿里云服务器买哪种好用点?
    python编写小游戏之三入最最简陋简单贪食蛇编写2
    hexo 使用hexo g -d报错
  • 原文地址:https://blog.csdn.net/qq_36978986/article/details/132902693