作者 RuoYi

富文本新增上传文件大小限制

@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 <div> 2 <div>
3 <el-upload 3 <el-upload
4 :action="uploadUrl" 4 :action="uploadUrl"
  5 + :before-upload="handleBeforeUpload"
5 :on-success="handleUploadSuccess" 6 :on-success="handleUploadSuccess"
6 :on-error="handleUploadError" 7 :on-error="handleUploadError"
7 name="file" 8 name="file"
@@ -46,6 +47,11 @@ export default { @@ -46,6 +47,11 @@ export default {
46 type: Boolean, 47 type: Boolean,
47 default: false, 48 default: false,
48 }, 49 },
  50 + // 上传文件大小限制(MB)
  51 + fileSize: {
  52 + type: Number,
  53 + default: 5,
  54 + },
49 /* 类型(base64格式、url格式) */ 55 /* 类型(base64格式、url格式) */
50 type: { 56 type: {
51 type: String, 57 type: String,
@@ -130,14 +136,6 @@ export default { @@ -130,14 +136,6 @@ export default {
130 this.quill.format("image", false); 136 this.quill.format("image", false);
131 } 137 }
132 }); 138 });
133 - // toolbar.addHandler("video", (value) => {  
134 - // this.uploadType = "video";  
135 - // if (value) {  
136 - // this.$refs.upload.$children[0].$refs.input.click();  
137 - // } else {  
138 - // this.quill.format("video", false);  
139 - // }  
140 - // });  
141 } 139 }
142 this.Quill.pasteHTML(this.currentValue); 140 this.Quill.pasteHTML(this.currentValue);
143 this.Quill.on("text-change", (delta, oldDelta, source) => { 141 this.Quill.on("text-change", (delta, oldDelta, source) => {
@@ -158,6 +156,18 @@ export default { @@ -158,6 +156,18 @@ export default {
158 this.$emit("on-editor-change", eventName, ...args); 156 this.$emit("on-editor-change", eventName, ...args);
159 }); 157 });
160 }, 158 },
  159 + // 上传前校检格式和大小
  160 + handleBeforeUpload(file) {
  161 + // 校检文件大小
  162 + if (this.fileSize) {
  163 + const isLt = file.size / 1024 / 1024 < this.fileSize;
  164 + if (!isLt) {
  165 + this.$message.error(`上传文件大小不能超过 ${this.fileSize} MB!`);
  166 + return false;
  167 + }
  168 + }
  169 + return true;
  170 + },
161 handleUploadSuccess(res, file) { 171 handleUploadSuccess(res, file) {
162 // 获取富文本组件实例 172 // 获取富文本组件实例
163 let quill = this.Quill; 173 let quill = this.Quill;