代码
class BrandUploader extends React.Component {
constructor({ value }) {
super();
this.state = {
previewVisible: false,
previewImage: '',
...this.initData(value)
};
}
componentWillReceiveProps({ value }) {
this.setState({
...this.initData()
});
}
initData = (value) => {
let fileList = (value|| []).map((item, i) => {
return {
uid: `temp${i}`,
name: item,
status: 'done',
url: item
};
});
return {
fileList,
value
};
}
handleCancel = () => this.setState({ previewVisible: false })
handlePreview = (file) => {
this.setState({
previewImage: file.url || file.thumbUrl,
previewVisible: true
});
}
handleChange = ({ fileList }) => {
this.setState({ fileList });
}
beforeUpload = (maxSize, file) => {
let reg = new RegExp(/^image\/\jpeg|gif|jpg|png$/, 'i');
if (reg.test(file.type)) {
if (file.size/1024 <= 200) {
return true;
} else {
message.info('上传文件过大');
return false;
}
} else {
message.info('图片格式不对');
return false;
}
}
render() {
const { previewVisible, previewImage, fileList } = this.state;
const { limit, maxSize, appPath='goods' } = this.props;
const uploadButton = (
<div>
<Icon type="plus" />
<div className="ant-upload-text">Upload</div>
</div>
);
return (
<div className="clearfix">
<Upload
name='file'
action='/resources-uploadfile/photo/photos'
listType="picture-card"
data={{ appPath: appPath }}
fileList={fileList}
beforeUpload={this.beforeUpload.bind(this, maxSize)}
onPreview={this.handlePreview}
onChange={this.handleChange}
>
{fileList.length >= 1 ? null : uploadButton}
</Upload>
<Modal visible={previewVisible} footer={null} onCancel={this.handleCancel}>
<img alt="example" style={{ width: '100%' }} src={previewImage} />
</Modal>
</div>
);
}
}
这不是你在beforeUpload里写的吗?直接把beforeUpload里的大小判断的代码去掉就可以了