Ant Design Upload 组件:如何阻止不符合类型的文件添加到列表
可以在 beforeUpload 函数中返回 false,表示不符合类型的文件不会被上传,也不会被添加到列表中。示例代码如下:
<Upload
beforeUpload={(file) => {
const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
if (!isJpgOrPng) {
message.error('只能上传 JPG/PNG 文件!');
return false;
}
return true;
}}
>
<Button icon={<UploadOutlined />}>上传文件</Button>
</Upload>
在这个示例中,如果上传的文件类型不是 JPG 或 PNG,会弹出错误提示,并且返回 false,文件不会被上传和添加到列表中。
原文地址: https://www.cveoy.top/t/topic/lHaH 著作权归作者所有。请勿转载和采集!