首页 > 解决方案 > 如何使用 Node.js 从“RichTextEditor”上传图像?

问题描述

我在我的项目中使用 RichTextEditor。如何上传图片并使用上传的图片网址?

http://joxi.ru/52aEa1jUkkw712

标签: node.jsvue.jsrich-text-editor

解决方案


<textarea id="rich_editor" placeholder="Type Message"></textarea>

<script>
   let editorcfg = {}
   editorcfg.file_upload_handler = uploadEditorImage();
   let editor = new RichTextEditor("#rich_editor", editorcfg);

   function uploadEditorImage(file, callback, optionalIndex, optionalFiles) {
       let url = "/notification/image/upload";
       let formData = new FormData();
       formData.append("image", file);

       axios.post(url, formData, {
           headers: {
              "Content-Type": "multipart/form-data"
           }
       }).then(response => {
            if (response.data.success) {
                 Swal.fire(
                     "Success!",
                     "Image has been uploaded.",
                     "success"
                 );

              callback(response.data.url);
           }
      });
  }    
</script>

推荐阅读