reactjs - AntD Upload在裁剪图片前需要验证
问题描述
我想上传方形图像,我想验证图像类型。我有以下代码。此代码在图像被裁剪后验证。但我想在图像加载之前验证模态中的裁剪。
AntD组件
<ImgCrop rotate>
<Upload
listType="picture-card"
customRequest={onFileChange}
fileList={fileList}
onChange={onChange}
onPreview={onPreview}
beforeUpload={beforeUpload}
maxCount={1}
onRemove={onRemove}
>
{fileList.length < 1 && "+ Upload"}
</Upload>
</ImgCrop>
验证功能
function beforeUpload(file) {
const isJpgOrPng = file.type === "image/jpeg" || file.type === "image/png";
if (!isJpgOrPng) {
message.error("You can only upload JPG/PNG file!");
}
const isLt2M = file.size / 1024 / 1024 < 2;
if (!isLt2M) {
message.error("Image must smaller than 2MB!");
}
return isJpgOrPng && isLt2M;
}
解决方案
您应该在ImgCrop中使用道具 beforeCrop 验证图像
beforeCrop:模态打开前调用,如果返回false,则不打开
推荐阅读
- python - 通过python套接字接收ctypes`c_wchar_p`的问题(python 3.7.7)
- python - 为什么Python中字典的键可以相同?
- javascript - 存在斜杠时如何在php中解析编码数组
- javascript - 在 Typescript 中使用 Ramda Pipe 输入错误
- reactjs - 选择 oauth2 flow spa 应用程序
- python - 对两个单独的图表强制执行相同的节点位置/布局
- amazon-web-services - Terraform RDS 数据库凭据
- python - Django循环导入错误,试图允许位置访问活动
- swiftui - 如何在 SwiftUI 中动态调整图像大小以实现可访问性?
- angularjs - 角度绑定到弹出框