uploadMixin.js 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. var uploadMixin = {
  2. methods: {
  3. initEditorOptions(editor, baseUrl) {
  4. const that = this;
  5. this.editor.config.zIndex = 333;
  6. this.editor.config.height = 400;
  7. this.editor.config.uploadImgMaxSize = 2 * 1024 * 1024;
  8. this.editor.config.uploadImgMaxLength = 5; // 一次最多上传 5 个图片
  9. this.editor.config.customUploadImg = function (resultFiles, insertImgFn) {
  10. resultFiles.forEach(function (file) {
  11. // resultFiles 是 input 中选中的文件列表
  12. // insertImgFn 是获取图片 url 后,插入到编辑器的方法
  13. that.uploadImage(file).then(res => {
  14. insertImgFn(res);
  15. })
  16. });
  17. }
  18. },
  19. // 上传图片
  20. uploadImage(file) {
  21. const formData = new FormData();
  22. formData.append('file', file);
  23. return new Promise(function (resolve, reject) {
  24. PublicApi.uploadimg(formData, function (res) {
  25. if (res.code === 0) {
  26. resolve(res.data);
  27. }
  28. reject()
  29. });
  30. })
  31. }
  32. }
  33. };