我尝试了下面的代码,这对我上传单个文件和更新上传的文件名很有效。
const props = {
name: 'file',
multiple: false,
showUploadList: {
showDownloadIcon: false,
},
onRemove: file => {
this.setState(state => {
return {
fileList: []
};
});
},
beforeUpload: file => {
this.setState(state => ({
fileList: [file]
}))
return false;
},
fileList
}
可以同时用于 Upload 和 Dragger
<Dragger {...props} >
<p className="ant-upload-drag-icon">
<Icon type="cloud-upload" />
</p>
<p className="ant-upload-text">Click or drag file to this area to upload</p>
</Dragger>
<Upload {...props} accept=".asc,.pem,.pub" style={{ width: '100%' }} tabIndex={3}>
<Button disabled={is_view} style={{ width: '100%' }}>
<Icon type="upload" />
{this.state.fileList && this.state.fileList[0] && this.state.fileList[0].name ? this.state.fileList[0].name : 'Click to Upload'}
</Button>
</Upload>
提交时-
const formData = new FormData()
formData.append('file', this.state.fileList[0])