From 71704264c5d54fe55409b412f782c9fd6edf8f15 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 4 Dec 2020 00:57:46 +0800 Subject: [PATCH 1/3] refactor: update upload component --- src/api/file.js | 6 +-- src/assets/scripts/uploadImageFile.js | 18 +------ src/assets/scripts/util.js | 7 +++ src/components/CodemirrorEditor/header.vue | 2 +- .../CodemirrorEditor/uploadImgDialog.vue | 51 ++++++++++++------- src/main.js | 1 - src/store/index.js | 1 - src/view/CodemirrorEditor.vue | 2 +- 8 files changed, 46 insertions(+), 42 deletions(-) diff --git a/src/api/file.js b/src/api/file.js index 25cfc91..54ff50b 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/uploadImageFile.js b/src/assets/scripts/uploadImageFile.js index 081b4b1..45883bf 100644 --- a/src/assets/scripts/uploadImageFile.js +++ b/src/assets/scripts/uploadImageFile.js @@ -2,12 +2,6 @@ import fileApi from "../../api/file"; export function uploadImgFile(file) { return new Promise((resolve, reject) => { - const checkImageResult = isImageIllegal(file); - if (checkImageResult) { - reject(checkImageResult); - return; - } - const base64Reader = new FileReader(); base64Reader.readAsDataURL(file); base64Reader.onload = function () { @@ -22,14 +16,4 @@ export function uploadImgFile(file) { }); }; }); -} - -export function isImageIllegal(file) { - if (!/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(file.name)) { - return "请上传 JPG/PNG/GIF 格式的图片"; - } - if (file.size > 5 * 1024 * 1024) { - return "由于公众号限制,图片大小不能超过 5.0M"; - } - return false; -} +} \ No newline at end of file diff --git a/src/assets/scripts/util.js b/src/assets/scripts/util.js index 7d5a5fe..f9f43a4 100644 --- a/src/assets/scripts/util.js +++ b/src/assets/scripts/util.js @@ -263,3 +263,10 @@ 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 diff --git a/src/components/CodemirrorEditor/header.vue b/src/components/CodemirrorEditor/header.vue index 27b62e1..b73844b 100644 --- a/src/components/CodemirrorEditor/header.vue +++ b/src/components/CodemirrorEditor/header.vue @@ -48,7 +48,7 @@ diff --git a/src/components/CodemirrorEditor/uploadImgDialog.vue b/src/components/CodemirrorEditor/uploadImgDialog.vue index 48bf255..a62fe72 100644 --- a/src/components/CodemirrorEditor/uploadImgDialog.vue +++ b/src/components/CodemirrorEditor/uploadImgDialog.vue @@ -29,7 +29,8 @@ :multiple="true" accept=".jpg, .jpeg, .png, .gif" name="file" - :before-upload="beforeUpload" + :before-upload="beforeImageUpload" + :http-request="uploadImage" v-loading="uploadingImg" > @@ -305,6 +306,8 @@