富文本 vue-quill-editor 图片上传

vue-quill-editor 原本的上传图片是将图片转为base64编码,当图片比较大时,会很卡。
所以要将图片上传到服务器,图片标签内只需要存储图片的网络地址就可以了。
在这里插入图片描述

具体实现

let param = new FormData(); //创建form对象
param.append('file',fileInput.files[0]); //通过append向form对象添加数据
axios.post('http://127.0.0.1:8081/upload',param).then(ret =>{
  var res=ret.data;
  if(res.code==1){
    Vue.prototype.$message({ message: res.msg||'上传成功!', type: 'success' });
  }else{
    Vue.prototype.$message({ message: res.msg, type: 'warning' });
  }                    
}).catch(function (error) { // 请求失败处理
    console.log(error);
    Vue.prototype.$message({ message: error, type: 'warning' });
});

完整代码:在quill.js中修改

BaseTheme.DEFAULTS = (0, _extend2.default)(true, {}, _theme2.default.DEFAULTS, {
  modules: {
    toolbar: {
      handlers: {
        formula: function formula() {
          this.quill.theme.tooltip.edit('formula');
        },
        image: function image() {
          var _this3 = this;

          var fileInput = this.container.querySelector('input.ql-image[type=file]');
          if (fileInput == null) {
            fileInput = document.createElement('input');
            fileInput.setAttribute('type', 'file');
            fileInput.setAttribute('accept', 'image/png, image/gif, image/jpeg, image/bmp, image/x-icon');
            fileInput.classList.add('ql-image');
            fileInput.addEventListener('change', function () {
              if (fileInput.files != null && fileInput.files[0] != null) {
                let param = new FormData(); //创建form对象
                param.append('file',fileInput.files[0]); //通过append向form对象添加数据
                axios.post('http://127.0.0.1:8081/upload',param).then(ret =>{
                  var res=ret.data;
                  if(res.code==1){
                    var range = _this3.quill.getSelection(true);
                    _this3.quill.updateContents(new _quillDelta2.default().retain(range.index).delete(range.length).insert({ image: res.data.url }), _emitter2.default.sources.USER);
                    _this3.quill.setSelection(range.index + 1);
                    fileInput.value = "";
                    Vue.prototype.$message({ message: res.msg||'上传成功!', type: 'success' });
                  }else{
                    Vue.prototype.$message({ message: res.msg, type: 'warning' });
                  }                    
              }).catch(function (error) { // 请求失败处理
                  console.log(error);
                  Vue.prototype.$message({ message: error, type: 'warning' });
              });
                // var reader = new FileReader();
                // reader.onload = function (e) {
                //   var range = _this3.quill.getSelection(true);
                //   _this3.quill.updateContents(new _quillDelta2.default().retain(range.index).delete(range.length).insert({ image: e.target.result }), _emitter2.default.sources.USER);
                //   _this3.quill.setSelection(range.index + 1, _emitter2.default.sources.SILENT);
                //   fileInput.value = "";
                // };
                // reader.readAsDataURL(fileInput.files[0]);
              }
            });
            this.container.appendChild(fileInput);
          }
          fileInput.click();
        },
        video: function video() {
          this.quill.theme.tooltip.edit('video');
        }
      }
    }
  }
});
  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值