/**
* 上传图片组件
* @param imgSize 图片大小限制
* @param data 上传数据
* @param disabled 是否禁用
*/
import React, { useState,useEffect } from 'react';
import { Upload, Icon, message} from 'antd';
const UploadImage = ({
imgSize = 50,
data = { Directory: 'Image' },
disabled = false,
onChange,
value
}) => {
const [loading, setLoading] = useState(false);
const [fileList, setFileList] = useState([]);
useEffect(()=>{
setImg();
},[value])
const beforeUpload = (file) => {
const isImage = file.type.startsWith('image/');
if (!isImage) {
message.error('只能上传图片文件!');
return false;
}
const isLt4M = file.size / 1024 < imgSize;
if (!isLt4M) {
message.error(`图片大小不能超过${imgSize}KB!`);
onChange('')
}
return isLt4M;
};
const uploadConfig = {
action: `${configs.host.test}/api/FileUpload/Upload`,
listType: 'picture-card',
onChange(info) {
setFileList(info.fileList);
if (info.file.status === 'uploading') {
setLoading(true);
return;
}
if (info.file.status === 'done') {
if (info.file.response.code === '0') {
message.success(`${info.file.name} 上传完成`);
onChange(info.file.response.data)
} else {
onChange('')
message.error(`${info.file.name} 上传失败`);
}
}
},
onRemove() {
setLoading(false);
onChange('')
},
};
const setImg = () => {
if(value){
setFileList([
{
uid: 1,
name: 'xxx.jpg',
status: 'done',
url: value,
},
]);
}else{
setFileList([]);
}
};
return (
<Upload
{...uploadConfig}
name="filename"
data={{ ...data, merchantId: localStorage.getItem('MerchantId') }}
defaultFileList={fileList}
fileList={fileList}
beforeUpload={beforeUpload}
disabled={disabled}
>
{fileList.length > 0 ? null : (
<div>
<Icon type={loading ? 'loading' : 'plus'} />
<div className="ant-upload-text">上传</div>
</div>
)}
</Upload>
);
};
export default UploadImage;
{/* 上传组件 ---开始 */}
<FormItem
{...formItemLayout2}
label="上传组件"
extra="图片建议尺寸:606像素*202像素,大小不超过2M。"
>
{getFieldDecorator("brandLogo", {
rules: [{ required: true, message: "请上传图片" }],
initialValue: detailData.brandLogo,
})(<UploadImage imgSize={10000} />)}
</FormItem>
{/* 上传组件 ---结束 */}
-
将上传组件和Form绑定在一起
-
不需要在提交的时候额外判断 XXX图片是否存在
-
删除图片时会主动触发表单当前图片的验证,提示用户,表现形式和其他表单项一致
-