将base64图片以formdata格式上传到图片服务器

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现将本地图片base64格式传到后端,可以使用以下步骤: 1. 在Vue项目中安装mavon-editor插件,可以使用npm进行安装:`npm install mavon-editor --save` 2. 在需要使用编辑器的Vue组件中引入mavon-editor并注册组件: ```vue <template> <mavon-editor v-model="content" @imgAdd="handleImgAdd"></mavon-editor> </template> <script> import 'mavon-editor/dist/css/index.css' import MavonEditor from 'mavon-editor' export default { components: { MavonEditor }, data() { return { content: '' } }, methods: { handleImgAdd(data) { // 处理图片上传 } } } </script> ``` 3. 在handleImgAdd方法中,可以使用FormData对象将base64格式图片数据上传到后端,代码如下: ```javascript handleImgAdd(data) { const file = data.file const reader = new FileReader() const vm = this reader.readAsDataURL(file) reader.onload = function() { const formData = new FormData() formData.append('image', this.result) axios.post('/upload-image', formData).then(response => { vm.content += '<img src="' + response.data.url + '">' }) } } ``` 上述代码中,我们使用FileReader对象将文件读取为base64格式,然后创建FormData对象,将base64数据作为参数添加到formData中。最后,使用axios库的post方法将formData发送到后端。当上传成功后,我们将回调函数中的response.data.url作为图片的src属性,插入到编辑器中。 4. 在后端接收base64格式图片数据,并将其转换为文件保存到服务器,最后返回图片的URL地址。这一部分需要根据后端框架和语言进行实现,这里不做过多介绍。 以上就是使用mavon-editor将本地图片base64格式传到后端并回显的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值