From 00fc92833dbb43d926d5fd5849443de147fdabf9 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 5 Dec 2020 21:16:09 +0800 Subject: [PATCH] fix: resolve upload error --- src/api/file.js | 6 ++-- src/assets/scripts/util.js | 35 ++++++++++++++---- .../CodemirrorEditor/uploadImgDialog.vue | 36 +++++++++++-------- src/main.js | 2 -- src/view/CodemirrorEditor.vue | 32 +++++------------ 5 files changed, 61 insertions(+), 50 deletions(-) diff --git a/src/api/file.js b/src/api/file.js index 54ff50b..25cfc91 100644 --- a/src/api/file.js +++ b/src/api/file.js @@ -237,9 +237,9 @@ function fileUpload(content, file) { case "github": return ghFileUpload(content, file.name); default: - return file.size / 1024 < 1024 - ? giteeUpload(content, file.name) - : ghFileUpload(content, file.name); + // return file.size / 1024 < 1024 + // ? giteeUpload(content, file.name) + // : ghFileUpload(content, file.name); return ghFileUpload(content, file.name); } } diff --git a/src/assets/scripts/util.js b/src/assets/scripts/util.js index f9f43a4..d1d974f 100644 --- a/src/assets/scripts/util.js +++ b/src/assets/scripts/util.js @@ -264,9 +264,32 @@ export function createTable({ data, rows, cols }) { return table; } -export const toBase64 = file => new Promise((resolve, reject) => { - const reader = new FileReader(); - reader.readAsDataURL(file); - reader.onload = () => resolve(reader.result.split(",").pop()); - reader.onerror = error => reject(error); -}); \ No newline at end of file +export const toBase64 = (file) => + new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.readAsDataURL(file); + reader.onload = () => resolve(reader.result.split(",").pop()); + reader.onerror = (error) => reject(error); + }); + +export function checkImage(file) { + // check filename suffix + const isValidSuffix = /\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(file.name); + if (!isValidSuffix) { + return { + ok: false, + msg: "请上传 JPG/PNG/GIF 格式的图片", + }; + } + + // check file size + const maxSize = 5; + const isLt5M = file.size / 1024 / 1024 <= maxSize; + if (!isLt5M) { + return { + ok: false, + msg: `由于公众号限制,图片大小不能超过 ${maxSize}M`, + }; + } + return { ok: true }; +} diff --git a/src/components/CodemirrorEditor/uploadImgDialog.vue b/src/components/CodemirrorEditor/uploadImgDialog.vue index 707bb03..4edef1d 100644 --- a/src/components/CodemirrorEditor/uploadImgDialog.vue +++ b/src/components/CodemirrorEditor/uploadImgDialog.vue @@ -304,6 +304,7 @@