美文网首页
vue-quill-editor如何把图片上传到服务器里面

vue-quill-editor如何把图片上传到服务器里面

作者: zZ_d205 | 来源:发表于2020-11-26 16:46 被阅读0次

默认vue-quill-editor上传之后是转成base64
废话少说,代码
你需要一个工具js quillConfig.js

import http from '../service/http';
import service from "../service/service";

/*富文本编辑图片上传配置*/
const uploadConfig = {
  action: 'common.uploadfile.single', // 必填参数 图片上传地址
  methods: 'POST', // 必填参数 图片上传方式
  token: '', // 可选参数 如果需要token验证,假设你的token有存放在sessionStorage
  name: 'img', // 必填参数 文件的参数名
  size: 1024, // 可选参数   图片大小,单位为Kb, 1M = 1024Kb
  accept: 'image/png, image/gif, image/jpeg, image/bmp, image/x-icon' // 可选 可上传的图片格式
};

// toolbar工具栏的工具选项(默认展示全部)
const toolOptions = [
  ['bold', 'italic', 'underline', 'strike'],
  ['blockquote', 'code-block'],
  [{
    'header': 1
  }, {
    'header': 2
  }],
  [{
    'list': 'ordered'
  }, {
    'list': 'bullet'
  }],
  [{
    'script': 'sub'
  }, {
    'script': 'super'
  }],
  [{
    'indent': '-1'
  }, {
    'indent': '+1'
  }],
  [{
    'direction': 'rtl'
  }],
  [{
    'size': ['small', false, 'large', 'huge']
  }],
  [{
    'header': [1, 2, 3, 4, 5, 6, false]
  }],
  [{
    'color': []
  }, {
    'background': []
  }],
  [{
    'font': []
  }],
  [{
    'align': []
  }],
  ['clean'],
  ['link', 'image', 'video']
];
const handlers = {
  image: function image() {
    var self = this;

    var fileInput = this.container.querySelector('input.ql-image[type=file]');
    if (fileInput === null) {
      fileInput = document.createElement('input');
      fileInput.setAttribute('type', 'file');
      // 设置图片参数名
      if (uploadConfig.name) {
        fileInput.setAttribute('name', uploadConfig.name);
      }
      // 可设置上传图片的格式
      fileInput.setAttribute('accept', uploadConfig.accept);
      fileInput.classList.add('ql-image');
      // 监听选择文件
      fileInput.addEventListener('change', function () {
        // 创建formData
        let formData = new FormData();
        console.log(fileInput);
        // formData.append('file', fileInput.files[0]);
        formData.append("filename", fileInput.files[0]);
        formData.append("token", JSON.parse(localStorage.getItem("userInfo")).token);
        fileInput.value = '';
        console.log(http);
        //这里 请求
        http.post('/Uploadfile/uploadpic', formData).then(res => {
          console.log(res);
          if (res.data.code == 200) {
            let imgUrl = res.data.result.file_url
            let picPath = service.baseUrl + imgUrl;
            let length = self.quill.getSelection(true).index;
            //这里很重要,你图片上传成功后,img的src需要在这里添加,res.path就是你服务器返回的图片链接。
            self.quill.insertEmbed(length, 'image', picPath);
            self.quill.setSelection(length + 1)
            fileInput.value = ''
          }
        })
        // http.post('/Uploadfile/uploadpic', formData, function(resp) {
        //   console.log(resp);
        //   var path = resp.data.ftpPath;
        //   var picPath = process.env.FTP_URL + path;
        //   let length = self.quill.getSelection(true).index;
        //   //这里很重要,你图片上传成功后,img的src需要在这里添加,res.path就是你服务器返回的图片链接。
        //   self.quill.insertEmbed(length, 'image', picPath);
        //   self.quill.setSelection(length + 1)
        //   fileInput.value = ''
        // }, formData);
      });
      console.log(fileInput);
      this.container.appendChild(fileInput);
    }
    fileInput.click();
  }
};

export default {
  placeholder: '',
  theme: 'snow', // 主题
  modules: {
    toolbar: {
      container: toolOptions, // 工具栏选项
      handlers: handlers // 事件重写
    }
  }
};

接着在你用的地方

  import quillConfig from "../../../utils/quillConfig";
  //富文本
  import 'quill/dist/quill.core.css';
  import 'quill/dist/quill.snow.css';
  import 'quill/dist/quill.bubble.css';
  import { quillEditor } from 'vue-quill-editor';
data:{
   return:{
        quillOption:quillConfig
    }
}
 components: {
      quillEditor
    },


              <quill-editor :options="quillOption" v-model="data.content" ref="myQuillEditor" @blur="onEditorBlur($event)" @focus="onEditorFocus($event)" @change="onEditorChange($event)">
              </quill-editor>

相关文章

网友评论

      本文标题:vue-quill-editor如何把图片上传到服务器里面

      本文链接:https://www.haomeiwen.com/subject/ouikiktx.html