Vue3 使用v-md-editor如何动态上传图片了

🚀 优质资源分享 🚀

学习路线指引(点击解锁)知识定位人群定位
🧡 Python实战微信订餐小程序 🧡

进阶级本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。
💛Python量化交易实战💛

入门级手把手带你打造一个易扩展、更安全、效率更高的量化交易系统

Vue3 使用v-md-editor如何动态上传图片了

前端代码:


|  | <v-md-editor | :autofocus="true" v-model="blog.content" height="510px" placeholder="&#x8BF7;&#x8F93;&#x5165;&#x5185;&#x5BB9;" left-toolbar="undo redo clear | h bold italic strikethrough quote | ul ol table hr | image | save " :disabled-menus="[]" @upload-image="handleUploadImage"> |
|  | v-md-editor> |

</v-md-editor>

按我以上配置后,你把图片插入或者从本地拖入,就可以激发 upload-image绑定的函数,我这的名称为: handleUploadImage

先看看这个图片编辑时的图片吧

Vue3 使用v-md-editor如何动态上传图片了

大致流程图

Vue3 使用v-md-editor如何动态上传图片了

在setup看看这个函数(这是我写好的)

files:表示你拖入的图片文件,可以是多个,也可以是一个

insertImage:url就表示你需要回显的地址;desc就是名称;markdown语法: ![desc](url)


|  | function handleUploadImage(event,insertImage,files){ |
|  | for(let i in files){ |
|  | const formData = new FormData(); |
|  |  formData.append('file', files[i]); |
|  |  proxy.$axios.post(${proxy.PATH}/uploadImg,formData).then( |
|  | response => { |
|  | insertImage({ |
|  | url:response.data, |
|  | desc: 'DESC', |
|  |  }) |
|  |  }, |
|  | error => { |
|  | console.log('&#x8BF7;&#x6C42;&#x5931;&#x8D25;&#x4E86;',error.message) |
|  |  } |
|  |  ) |
|  |  } |
|  | } |

有没有人和我一样去看过这个file长什么样子

Vue3 使用v-md-editor如何动态上传图片了

可以看到我上面发送了一个请求到后端,标签带上了这个文件;就是下面这段代码


|  |  proxy.$axios.post(${proxy.PATH}/uploadImg,formData).then( |
|  | response => { |
|  | insertImage({ |
|  | url:response.data, |
|  | desc: 'DESC', |
|  |  }) |
|  |  }, |
|  | error => { |
|  | console.log('&#x8BF7;&#x6C42;&#x5931;&#x8D25;&#x4E86;',error.message) |
|  |  } |
|  |  ) |

后端的代码:

请求的位置


|  | package com.mhy.blog.controller; |
|  | import com.mhy.blog.utils.ConstUtils; |
|  | import com.mhy.blog.utils.FileUtils; |
|  | import org.springframework.web.bind.annotation.PostMapping; |
|  | import org.springframework.web.bind.annotation.RequestParam; |
|  | import org.springframework.web.bind.annotation.RestController; |
|  | import org.springframework.web.multipart.MultipartFile; |
|  | import java.util.Objects; |
|  |  |
|  | @RestController |
|  | public class FileController { |
|  | @PostMapping("/uploadImg") |
|  | public String img(@RequestParam(value = "file") MultipartFile file){ |
|  | try{ |
|  | return FileUtils.uploadImg(file, |
|  |  ConstUtils.SAVE\_IMG\_PATH, |
|  |  Objects.requireNonNull(file.getOriginalFilename()), |
|  |  ConstUtils.REQUEST\_IMG\_PATH); |
|  |  }catch (Exception e){ |
|  |  e.printStackTrace(); |
|  | return ConstUtils.IMG\_UPLOAD\_FAILURE; |
|  |  } |
|  |  } |
|  | } |
|  |  |

注意里面有我定制的一些常量:


|  | public static String SAVE\_IMG\_PATH = "D:\\JAVAIDEA\\Blog\\java\\blog\\src\\main\\resources\\public\\img\\"; |
|  |  |
|  | public static String REQUEST\_IMG\_PATH = "http://localhost:8888/blog/img/"; |
|  | public static String IMG\_UPLOAD\_FAILURE = "&#x56FE;&#x7247;&#x4E0A;&#x4F20;&#x5931;&#x8D25;"; |

看看这个工具方法: FileUtils.uploadImg


|  | package com.mhy.blog.utils; |
|  |  |
|  | import org.springframework.web.multipart.MultipartFile; |
|  |  |
|  | import java.io.File; |
|  | import java.util.Random; |
|  | import java.util.UUID; |
|  |  |
|  |  |
|  | public class FileUtils { |
|  |  |
|  | public static String uploadImg(MultipartFile file, String path1, String name, String path2){ |
|  | // &#x901A;&#x8FC7;uuid&#x4EA7;&#x751F;&#x4E00;&#x4E2A;&#x56FE;&#x7247;&#x540D;&#x5B57; |
|  | String uuid = UUID.randomUUID().toString().trim().replaceAll("-",""); |
|  | String imgName = uuid + name; |
|  | // &#x8FD9;&#x662F;&#x6211;&#x968F;&#x673A;&#x9009;&#x62E9;&#x4E86;&#x4E00;&#x6587;&#x4EF6;&#x5939; &#x8FD9;&#x91CC;&#x53EA;&#x662F;&#x6211;&#x4E2A;&#x4EBA;&#x7231;&#x597D; |
|  | String code = Integer.toString(new Random().nextInt(5) + 1); |
|  | // &#x62FC;&#x63A5;&#x8DEF;&#x5F84; |
|  | String imgPath = path1 + "img" + code + "\\"; |
|  | String requestPath = path2 + "img" + code + "/"; |
|  |  |
|  | try { |
|  | // &#x4E0A;&#x4F20;&#x64CD;&#x4F5C; |
|  | File imgFile = new File(imgPath, imgName); |
|  |  file.transferTo(imgFile); |
|  |  } catch (Exception e) { |
|  |  e.printStackTrace(); |
|  |  } |
|  | // &#x8FD4;&#x56DE;&#x56FE;&#x7247;&#x7684;&#x8DEF;&#x5C31; |
|  | return requestPath + imgName; |
|  |  } |
|  | } |

后端图片的位置

Vue3 使用v-md-editor如何动态上传图片了

测试:

Vue3 使用v-md-editor如何动态上传图片了

顺便附带上我的main.js吧


|  | // markdown &#x7684;&#x5F15;&#x5165; |
|  | import VueMarkdownEditor from '@kangc/v-md-editor' |
|  | import '@kangc/v-md-editor/lib/style/base-editor.css' |
|  | import VMdpreview from '@kangc/v-md-editor/lib/preview' |
|  | import vuepressTheme from '@kangc/v-md-editor/lib/theme/vuepress.js' |
|  | import '@kangc/v-md-editor/lib/theme/style/vuepress.css' |
|  |  |
|  | // github&#x4E3B;&#x9898; |
|  | import githubTheme from '@kangc/v-md-editor/lib/theme/github.js' |
|  | import '@kangc/v-md-editor/lib/theme/style/github.css' |
|  | // &#x5F15;&#x5165; highlight&#x6838;&#x5FC3;&#x4EE3;&#x7801; |
|  | import hljs from 'highlight.js/lib/core' |
|  | // &#x5F15;&#x5165;&#x4EE3;&#x7801;&#x9AD8;&#x4EAE; |
|  | import json from 'highlight.js/lib/languages/json' |
|  | import java from 'highlight.js/lib/languages/java' |
|  | import javascript from 'highlight.js/lib/languages/javascript' |
|  | import c from 'highlight.js/lib/languages/c' |
|  | import cpp from 'highlight.js/lib/languages/cpp' |
|  | import armasm from 'highlight.js/lib/languages/armasm' |
|  | // &#x6309;&#x9700;&#x5F15;&#x5165; &#x4EE3;&#x7801;&#x9AD8;&#x4EAE; |
|  | hljs.registerLanguage('json',json) |
|  | hljs.registerLanguage('java',java) |
|  | hljs.registerLanguage('javascript',javascript) |
|  | hljs.registerLanguage('c',c) |
|  | hljs.registerLanguage('cpp',cpp) |
|  | hljs.registerLanguage('armasm',armasm) |
|  |  |
|  | // &#x914D;&#x7F6E; |
|  | VMdpreview.use(vuepressTheme) |
|  | VueMarkdownEditor.use(githubTheme,{ |
|  | Hljs: hljs, |
|  | extend(md){ |
|  | // &#x6269;&#x5C55;&#x63D2;&#x4EF6; |
|  |  } |
|  | }) |
|  |  |
|  | const app = createApp(App) |
|  | app.use(VueMarkdownEditor) |
|  | app.use(VMdpreview) |
|  | app.mount('#app') |

Original: https://blog.csdn.net/u012804784/article/details/126204128
Author: u012804784
Title: Vue3 使用v-md-editor如何动态上传图片了

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/748717/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球