From 043daac375b3b7d8de5963709269f18155151e8d Mon Sep 17 00:00:00 2001 From: LiuJiaNan <15703339975@163.com> Date: Tue, 28 Oct 2025 14:08:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96Upload=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E5=88=86=E8=BE=A8=E7=8E=87=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/Upload/index.js | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/components/Upload/index.js b/components/Upload/index.js index 7878dba..dd9f59a 100644 --- a/components/Upload/index.js +++ b/components/Upload/index.js @@ -73,17 +73,31 @@ const Upload = (props) => { // 验证图片分辨率 if (ratioArr.length === 2 && file.type?.startsWith("image/")) { - const img = new Image(); - img.src = file.url || file.thumbUrl; - img.onload = () => { - if (img.width !== +ratioArr[0] || img.height !== +ratioArr[1]) { - message.warning(`只能上传${ratio}分辨率的图片`); - const filtered = fileList.filter(item => item.uid !== file.uid); - onChange?.(filtered); - return; - } - onChange?.(fileList); + const validateImageResolution = (imageUrl) => { + const img = new Image(); + img.onload = () => { + if (img.width !== +ratioArr[0] || img.height !== +ratioArr[1]) { + message.warning(`只能上传${ratio}分辨率的图片`); + const filtered = fileList.filter(item => item.uid !== file.uid); + onChange?.(filtered); + return; + } + onChange?.(fileList); + }; + img.src = imageUrl; }; + + // 如果有现成的URL则直接使用,否则使用FileReader读取本地文件 + if (file.url) { + validateImageResolution(file.url); + } + else { + const reader = new FileReader(); + reader.onload = (e) => { + validateImageResolution(e.target.result); + }; + reader.readAsDataURL(file); + } } else { onChange?.(fileList);