Skip to content

Commit 7a43a22

Browse files
committed
feat: add image upload format and size limitation
受公众号限制,上传图片的大小不能超过 5M
1 parent 1be928c commit 7a43a22

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

assets/scripts/editor.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ let app = new Vue({
8484
let item = e.clipboardData.items[i];
8585
if (item.kind === 'file') {
8686
const pasteFile = item.getAsFile();
87+
if (!(this.checkType(pasteFile) && this.checkImageSize(pasteFile))) {
88+
return;
89+
}
8790
let data = new FormData();
8891
data.append('file', pasteFile);
8992
axios.post('https://imgkr.com/api/files/upload', data, {
@@ -154,6 +157,34 @@ let app = new Vue({
154157
});
155158
this.refresh();
156159
},
160+
// 图片上传前的处理
161+
beforeUpload(file) {
162+
return this.checkType(file) && this.checkImageSize(file);
163+
},
164+
// 检查文件类型
165+
checkType(file) {
166+
if (!/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(file.name)) {
167+
this.$message({
168+
showClose: true,
169+
message: '请上传 JPG/PNG/GIF 格式的图片',
170+
type: 'error'
171+
});
172+
return false;
173+
}
174+
return true;
175+
},
176+
// 检查图片大小
177+
checkImageSize(file) {
178+
if (file.size > 5 * 1024 * 1024) {
179+
this.$message({
180+
showClose: true,
181+
message: '由于公众号限制,图片大小不能超过 5.0M',
182+
type: 'error'
183+
});
184+
return false;
185+
}
186+
return true;
187+
},
157188
// 图片上传结束
158189
uploaded(response, file, fileList) {
159190
if (response.success) {
@@ -226,7 +257,6 @@ let app = new Vue({
226257
this.editor.replaceSelection(`\n${table}\n`, cursor);
227258
this.dialogFormVisible = false
228259
this.refresh();
229-
230260
},
231261
statusChanged() {
232262
this.refresh();

index.html

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@
7373
<el-header class="top">
7474
<!-- 图片上传 -->
7575
<el-upload action="https://imgkr.com/api/files/upload" headers="{'Content-Type': 'multipart/form-data'}"
76-
:show-file-list="false" :multiple="true" accept=".jpg,.jpeg,.png,.gif" name="file" :on-success="uploaded">
76+
:show-file-list="false" :multiple="true" accept=".jpg,.jpeg,.png,.gif" name="file"
77+
:before-upload="beforeUpload" :on-success="uploaded">
7778
<el-tooltip class="item" effect="dark" content="上传图片" placement="bottom-start">
7879
<i class="el-icon-upload" size="medium">&nbsp;</i>
7980
</el-tooltip>
@@ -175,7 +176,7 @@ <h3>一款高度简洁的微信 Markdown 编辑器</h3>
175176
<el-dialog title="插入表格" :visible.sync="dialogFormVisible">
176177
<el-form :model="form">
177178
<el-form-item label="行数(表头不计入行数)">
178-
<el-input v-model="form.rows"></el-input>
179+
<el-input v-model="form.rows"></el-input>
179180
</el-form-item>
180181
<el-form-item label="列数">
181182
<el-input v-model="form.cols"></el-input>
@@ -214,9 +215,9 @@ <h3>一款高度简洁的微信 Markdown 编辑器</h3>
214215
<script src="assets/scripts/editor.js"></script>
215216
<script>
216217
$('#loading').hide();
217-
window.console
218-
&& window.console.log
219-
&& (console.log("Think big, train fast, learn deep. See https://github.com/yanglbme"))
218+
window.console
219+
&& window.console.log
220+
&& (console.log("Think big, train fast, learn deep. See https://github.com/yanglbme"))
220221
</script>
221222
</body>
222223

0 commit comments

Comments
 (0)