<Upload {…uploadConfig} loading={loading}>
<Button style={{margin:‘15px 0 0 20px’}} type=“primary”>
{F({ id: ‘common.upload’ })}
const uploadConfig = {
accept: ‘.xlsx,.xls’,
action: ‘#’,
fileList: [],
multiple: false,
showUploadList: false,
beforeUpload: (file) => {
const transform = (raw) => {
let vinList = raw
if (_.isString(raw)) {
vinList = preVins(raw);
}
const str1 = /1{11}[0-9]{6}$/;
let vins = [];
if (vinList.length > 0) {
const isVinError = vinList.every((item) => str1.test(item));
if (!isVinError) {
const VinErrorList = vinList.filter((item) => !str1.test(item));
notification.error({
message: F({ id: ‘vehicle.upload-formatV2’ }, { name: VinErrorList.join() }),
});
}else {
vins = vinList;
}
} else {
notification.error({
message: F({ id: ‘vehicle.upload-format’ }),
});
};
this.setState({
fileList: […vins]
})
}
const isExcel = .endsWith(file.name, ‘.xls’) || .endsWith(file.name, ‘.xlsx’);
if(isExcel) {
const reader = new FileReader();
reader.onload = (e) => {
const data = e?.target?.result;
const workbook = window.XLSX.read(data, { type: ‘binary’ });
const { Sheet1 } = workbook.Sheets;
if (!Sheet1) {
notification.error({
message: “这个excel sheet1没有数据”,
});
this.setState({
fileList: [],
fileSuccess: false
})
return false
}
const vinList = Object.keys(Sheet1).reduce((previous, field) => {
const row = Sheet1[field]
if(.trim(row.h)) {
previous.push(.trim(row.h))
}
return previous
}, [])
transform(vinList)
};
reader.readAsBinaryString(file);
}else {
notification.warning({
message: F({ id: ‘common.unsupported.file.format’ }),
});
}
return true;
},
onProgress: (file) => {
this.setState({
loading: true,
percent: Math.floor(file.percent),
});
},
onSuccess: (res) => {
let fileType = false;
if (res.success || !res.sub_msg) {
if(fileSuccess !== false) {
fileType = true;
}
} else {
notification.error({
message: res.sub_msg || res.msg
});
};
this.setState({
loading: false,
fileSuccess: fileType
})
},
onError: (err, data) => {
if (err.status === 504) {
notification.success({
message: F({ id: ‘common.success.upload.504’ }),
});
return;
}
if (data) {
notification.error(data);
}
}
};
0-9A-Z ↩︎