优化Upload
parent
04f20eabfd
commit
ef1d0cd01d
|
|
@ -145,43 +145,57 @@ const Upload = (props) => {
|
|||
const ratioArr = ratio ? ratio.split("*") : [];
|
||||
const maxSize = size * 1024 * 1024;
|
||||
|
||||
// 对整个fileList进行验证和过滤
|
||||
const validateFiles = async (files) => {
|
||||
const validFiles = [];
|
||||
const invalidFiles = [];
|
||||
// 获取新增的文件(只验证新文件,不重复验证已存在的文件)
|
||||
const existingUids = new Set(value.map(f => f.uid));
|
||||
const newFiles = fileList.filter(file => !existingUids.has(file.uid));
|
||||
|
||||
for (const fileItem of files) {
|
||||
// 如果没有新文件,直接更新
|
||||
if (newFiles.length === 0) {
|
||||
onChange?.(fileList);
|
||||
return;
|
||||
}
|
||||
|
||||
// 验证文件格式
|
||||
const validateFileFormat = (fileItem) => {
|
||||
const suffix = fileItem.name?.substring(
|
||||
fileItem.name.lastIndexOf("."),
|
||||
fileItem.name.length,
|
||||
) || "";
|
||||
|
||||
// 验证文件格式
|
||||
if (acceptList.length > 0 && !acceptList.includes(suffix)) {
|
||||
invalidFiles.push(fileItem);
|
||||
// 只提示未提示过的文件
|
||||
if (!warnedFileUids.current.has(fileItem.uid)) {
|
||||
warnedFileUids.current.add(fileItem.uid);
|
||||
message.warning(`${fileItem.name}:只能上传${acceptTip}格式的文件`);
|
||||
}
|
||||
continue;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
// 验证文件大小
|
||||
const validateFileSize = (fileItem) => {
|
||||
if (maxSize && fileItem.size > maxSize) {
|
||||
invalidFiles.push(fileItem);
|
||||
// 只提示未提示过的文件
|
||||
if (!warnedFileUids.current.has(fileItem.uid)) {
|
||||
warnedFileUids.current.add(fileItem.uid);
|
||||
message.warning(`${fileItem.name}:文件大小不能超过${size}M`);
|
||||
}
|
||||
continue;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
// 验证图片分辨率
|
||||
const validateImageResolution = (fileItem) => {
|
||||
return new Promise((resolve) => {
|
||||
if (ratioArr.length !== 2 || !fileItem.type?.startsWith("image/")) {
|
||||
resolve(true);
|
||||
return;
|
||||
}
|
||||
|
||||
// 验证图片分辨率(异步)
|
||||
if (ratioArr.length === 2 && fileItem.type?.startsWith("image/")) {
|
||||
const validateImageResolution = (imageUrl) => {
|
||||
return new Promise((resolve) => {
|
||||
const checkResolution = (imageUrl) => {
|
||||
return new Promise((resolveInner) => {
|
||||
const img = new Image();
|
||||
img.onload = () => {
|
||||
const isValid = img.width === +ratioArr[0] && img.height === +ratioArr[1];
|
||||
|
|
@ -192,28 +206,50 @@ const Upload = (props) => {
|
|||
message.warning(`${fileItem.name}:只能上传${ratio}分辨率的图片`);
|
||||
}
|
||||
}
|
||||
resolve(isValid);
|
||||
resolveInner(isValid);
|
||||
};
|
||||
img.onerror = () => resolve(false);
|
||||
img.onerror = () => resolveInner(false);
|
||||
img.src = imageUrl;
|
||||
});
|
||||
};
|
||||
|
||||
const validResolution = fileItem.url
|
||||
? await validateImageResolution(fileItem.url)
|
||||
: await new Promise((resolve) => {
|
||||
if (fileItem.url) {
|
||||
checkResolution(fileItem.url).then(resolve);
|
||||
}
|
||||
else {
|
||||
const reader = new FileReader();
|
||||
reader.onload = (e) => {
|
||||
validateImageResolution(e.target.result).then(resolve);
|
||||
checkResolution(e.target.result).then(resolve);
|
||||
};
|
||||
reader.onerror = () => resolve(false);
|
||||
reader.readAsDataURL(fileItem.originFileObj);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
if (!validResolution) {
|
||||
// 只验证新增的文件
|
||||
const validateNewFiles = async (files) => {
|
||||
const validFiles = [];
|
||||
const invalidFiles = [];
|
||||
|
||||
for (const fileItem of files) {
|
||||
// 验证文件格式
|
||||
if (!validateFileFormat(fileItem)) {
|
||||
invalidFiles.push(fileItem);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 验证文件大小
|
||||
if (!validateFileSize(fileItem)) {
|
||||
invalidFiles.push(fileItem);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 验证图片分辨率(异步)
|
||||
const isValidResolution = await validateImageResolution(fileItem);
|
||||
if (!isValidResolution) {
|
||||
invalidFiles.push(fileItem);
|
||||
continue;
|
||||
}
|
||||
|
||||
validFiles.push(fileItem);
|
||||
|
|
@ -223,8 +259,13 @@ const Upload = (props) => {
|
|||
};
|
||||
|
||||
// 执行验证并更新
|
||||
validateFiles(fileList).then(({ validFiles }) => {
|
||||
onChange?.(validFiles);
|
||||
validateNewFiles(newFiles).then(({ validFiles: validNewFiles }) => {
|
||||
// 合并已存在的文件和通过验证的新文件
|
||||
const finalFileList = [
|
||||
...value, // 已存在的文件
|
||||
...validNewFiles, // 通过验证的新文件
|
||||
];
|
||||
onChange?.(finalFileList);
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue