我正在使用简单的antd Uploader dragger组件和传递道具beforeUpload,以避免上传我想要上传的文件扩展名。代码如下:
const props = {
name: "file",
multiple: true,
action: "/upload",
beforeUpload: (file) => {
if (file.name.split(".").reverse()[0] !== ("pdf" || "doc" || "docx")) {
console.log(`${file.name} is not a correct format file`);
}
return file.name.split(".").reverse()[0] === ("pdf" || "doc" || "docx") ? true : Upload.LIST_IGNORE;
},
onChange(info) {
const { status } = info.file;
console.log(info.files) //All format files are shown in console
},
accept: ".pdf,.doc,.docx",
showUploadList: false,
fileList: files,
};
这是组件的返回
<Upload.Dragger {...props}>
<p>Drag and upload your files</p>
</Upload.Dragger>
问题是,即使控制台正常工作并显示某个特定文件不是正确格式的文件,它仍然会被添加到不应该添加到fileList数组中,因为我正在对它使用Upload.LIST_IGNORE。我已经尝试调试了几个小时,但没有用,谁能指导我关于这个问题,我将不胜感激。
发布于 2021-03-11 19:05:06
在您当前的实现中,您只是将上传文件的文件类型与pdf
类型进行比较,因为您所有的文件格式都在括号中,并且您使用的是||
,它将返回第一个真值,在本例中为pdf
。
您需要使用您接受的文件格式列表检查上传的文件类型。
beforeUpload: (file) => {
const acceptedFormats = ['pdf', 'doc', 'docx'];
return acceptedFormats.includes(file.name.split('.')[1]) ? true : Upload.LIST_IGNORE;
},
https://stackoverflow.com/questions/66588707
复制