解决vue3中 wangeditor插入图片 formdata 传值为空值的问题(已解决)

解决vue3中 wangeditor插入图片 formdata 传值为空值的问题(已解决)

问题背景:

目前开发的项目中有一个富文本插入图片的需求,我使用的富文本是 wangeditor/editor,在使用其本身上传图片的功能,出现了formdata参数为空的情况,下面的解决方法完美解决了wangeditor富文本插入图片所遇到的formdata参数为空的问题

  • 原本的代码写法
 async customUpload(file, insertFn) {
              let image = new Image()
              const formData = new FormData()
              formData.append('file', file)
              try {
                // 这里打印的formData是{}
                const data = await uploadCover(formData)
                if (data.resp_code === 0) {
                  image.onload = () => {
                    data.datas += `?width=${image.width}&height=${image.height}`
                    insertFn(joinPath(data.datas), file.name, '')
                    that.$message({
                      message: '插入成功',
                      type: 'success',
                      showClose: true
                    })
                    image = null
                  }
                  image.onerror = () => {
                    that.$message({
                      message: data.resp_msg,
                      type: 'warning',
                      showClose: true
                    })
                  }
                  image.src = joinPath(data.datas)
                } else {
                  that.$message({
                    message: data.resp_msg,
                    type: 'warning',
                    showClose: true
                  })
                }
              } catch (uploadErr) {
                that.$message({
                  message:
                    uploadErr.resp_msg ||
                    uploadErr.error ||
                    '网络似乎不通畅,请检查后再试',
                  type: 'warning',
                  showClose: true
                })
              }
  • 更改过后的代码
async customUpload(file, insertFn) {
            const formData = new FormData();
            formData.append("file", file);
            axios({ // 使用原生的axios进行调用接口 就可以解决问题
              method: "post",
              headers: {
                Authorization: `Bearer ${getToken()}`
              },
              url: `${VITE_GLOB_API_URL}/上传图片后端的接收路径`,
              data: formData
            })
              .then(data => {
                if (data.data.resp_code === 0) {
                  insertFn(`${domain.value}${data.data.datas}`, file.name, "");
                  ElMessage({
                    message: "插入成功",
                    type: "success",
                    showClose: true
                  });
                } else {
                  ElMessage({
                    message: `${data.data.resp_msg}`,
                    type: "warning",
                    showClose: true
                  });
                }
              })
              .catch(error => {
                ElMessage({
                  message:
                    error.resp_msg ||
                    error.error ||
                    "网络似乎不通畅,请检查后再试",
                  type: "warning",
                  showClose: true
                });
              });
  • 最终效果
    ppxZy24.jpg
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值