文件上传修改
This commit is contained in:
parent
5fa91a7240
commit
fb2e787a47
|
|
@ -85,14 +85,12 @@ export default {
|
||||||
previewFileType: '',
|
previewFileType: '',
|
||||||
isUploading: false, // 添加上传状态标识
|
isUploading: false, // 添加上传状态标识
|
||||||
defaultFileSize: 1024 * 1024 * 5, // 默认文件大小5MB 超过5MB算大文件上传
|
defaultFileSize: 1024 * 1024 * 5, // 默认文件大小5MB 超过5MB算大文件上传
|
||||||
fileValidationMap: new Map(), // 存储文件验证结果
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
beforeUpload(file) {
|
beforeUpload(file) {
|
||||||
// 如果正在上传中,阻止新的上传
|
// 如果正在上传中,阻止新的上传
|
||||||
if (this.isUploading) {
|
if (this.isUploading) {
|
||||||
this.fileValidationMap.set(file.uid, false);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -105,23 +103,16 @@ export default {
|
||||||
|
|
||||||
// 验证文件大小
|
// 验证文件大小
|
||||||
const isLtMaxSize = file.size / 1024 / 1024 < this.maxSizeMB;
|
const isLtMaxSize = file.size / 1024 / 1024 < this.maxSizeMB;
|
||||||
|
|
||||||
let isValid = true;
|
|
||||||
|
|
||||||
if (!isAllowedType || !isAllowedMimeType) {
|
if (!isAllowedType || !isAllowedMimeType) {
|
||||||
this.$message.error(`只能上传 ${this.uploadType} 格式的文件!`);
|
this.$message.error(`只能上传 ${this.uploadType} 格式的文件!`);
|
||||||
isValid = false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isLtMaxSize) {
|
if (!isLtMaxSize) {
|
||||||
this.$message.error(`文件大小不能超过 ${this.maxFileTips}!`);
|
this.$message.error(`文件大小不能超过 ${this.maxFileTips}!`);
|
||||||
isValid = false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 存储验证结果
|
|
||||||
this.fileValidationMap.set(file.uid, isValid);
|
|
||||||
|
|
||||||
// 返回 false 阻止自动上传,我们将手动控制上传流程
|
|
||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
// 文件状态改变
|
// 文件状态改变
|
||||||
|
|
@ -131,20 +122,7 @@ export default {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查文件验证结果
|
// 不使用深拷贝,直接使用 fileList,但确保 files 是响应式的
|
||||||
const isValid = this.fileValidationMap.get(file.uid);
|
|
||||||
if (isValid === false) {
|
|
||||||
// 验证失败,从文件列表中移除该文件
|
|
||||||
const fileIndex = fileList.findIndex(item => item.uid === file.uid);
|
|
||||||
if (fileIndex !== -1) {
|
|
||||||
fileList.splice(fileIndex, 1);
|
|
||||||
}
|
|
||||||
this.fileValidationMap.delete(file.uid);
|
|
||||||
this.files = this.formatFileList(fileList);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 验证通过,继续处理
|
|
||||||
this.files = this.formatFileList(fileList);
|
this.files = this.formatFileList(fileList);
|
||||||
|
|
||||||
// 生成预览
|
// 生成预览
|
||||||
|
|
@ -162,7 +140,7 @@ export default {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果启用自动上传且文件验证通过,触发上传
|
// 如果启用自动上传且文件验证通过,触发上传
|
||||||
if (this.autoUpload && file.status === 'ready' && !this.isUploading && isValid !== false) {
|
if (this.autoUpload && file.status === 'ready' && !this.isUploading) {
|
||||||
if (this.fileUploadRule.fields_json) {
|
if (this.fileUploadRule.fields_json) {
|
||||||
// 文件需要ocr识别
|
// 文件需要ocr识别
|
||||||
this.uploadFile(file, '识别中');
|
this.uploadFile(file, '识别中');
|
||||||
|
|
@ -171,9 +149,6 @@ export default {
|
||||||
this.uploadFile(file, '上传中');
|
this.uploadFile(file, '上传中');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 清理验证结果
|
|
||||||
this.fileValidationMap.delete(file.uid);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// 格式化文件列表,确保数据结构正确
|
// 格式化文件列表,确保数据结构正确
|
||||||
|
|
@ -360,13 +335,7 @@ export default {
|
||||||
|
|
||||||
// 手动触发新文件的上传流程
|
// 手动触发新文件的上传流程
|
||||||
const newFile = files[0];
|
const newFile = files[0];
|
||||||
|
this.beforeUpload(newFile); // 先进行验证
|
||||||
// 先进行验证
|
|
||||||
const isValid = this.beforeUpload(newFile);
|
|
||||||
|
|
||||||
if (!isValid) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 创建新的文件对象,确保包含 percentage
|
// 创建新的文件对象,确保包含 percentage
|
||||||
const newFileObj = {
|
const newFileObj = {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue