<Upload
name="file"
//除了参数file: (binary) ,额外添加的type参数
data={{ type: "excel" }}
showUploadList={false}
accept=".xlsx,.xls"
multiple
action={`${window.config.serverOne}/api/v1/business/dict/importDict`}
method="POST"
headers={{
Authorization: users && `Bearer ${users.access_token}`,
}}
//这是判断上传文件的状态的方法
onChange={(info) => this.onChange(info)}
>
<Button type="primary" style={{ width: 100, marginLeft: 8, marginRight: 8 }}>导入Excel</Button>
</Upload>
//监听上传状态
onChange = async (info: any) => {
console.log(info, "info---->");
}
// 当一个页面有2个导入时,判断状态可传2个参数
/** 判断文件上传状态 */
onChange = async (info: any, idx: number) => {
if (info?.file?.status === "uploading") {
if (idx === 1) {
this.setState({ loadingG: true })
} else {
this.setState({ loading: true })
}
}
if (info?.file?.status === "done") {
if (idx === 1) {
this.setState({ loadingG: false })
} else {
this.setState({ loading: false })
}
if (info?.file?.response?.msg === "ok") {
message.success("文件上传成功")
} else {
message.error("文件上传失败")
}
} else if (info?.file?.status === "error") {
message.error("文件上传失败")
if (idx === 1) {
this.setState({ loadingG: false })
} else {
this.setState({ loading: false })
}
}
}
post接口 ,上传参数在Formdata
切记: upload上传到后台服务器和上传到阿里云服务器都是一样的写法