asb_cloud_front/src/components/UploadForm.jsx

45 lines
1.3 KiB
React
Raw Normal View History

2021-08-17 16:46:46 +05:00
import { Upload, Button } from 'antd'
import { UploadOutlined } from '@ant-design/icons'
2021-08-17 10:46:28 +05:00
import { useState } from 'react'
import { upload } from './factory'
export default function UploadForm({url, onUploadStart, onUploadComplete, onUploadError}) {
2021-08-17 16:46:46 +05:00
const [fileList, setfileList] = useState([])
2021-08-17 10:46:28 +05:00
const handleFileSend = async (values) => {
if(onUploadStart)
onUploadStart()
try {
const formData = new FormData()
2021-08-17 16:46:46 +05:00
fileList.forEach((val) => {
2021-08-17 10:46:28 +05:00
formData.append("files", val.originFileObj);
});
await upload(url, formData)
} catch(error) {
if(onUploadError)
onUploadError(error)
} finally {
2021-08-17 16:46:46 +05:00
setfileList([])
2021-08-17 10:46:28 +05:00
if(onUploadComplete)
onUploadComplete()
}
}
2021-08-17 16:46:46 +05:00
const isSendButtonEnabled = fileList.length > 0
2021-08-17 10:46:28 +05:00
return(
2021-08-17 16:46:46 +05:00
<div style={{display: 'flex'}}>
<Upload
name ="file"
fileList={fileList}
onChange={(props) => setfileList(props.fileList)}>
<Button icon={<UploadOutlined/>}>Загрузить файл</Button>
</Upload>
<Button type="primary"
disabled={!isSendButtonEnabled}
style={{marginLeft: '10px', display:isSendButtonEnabled?'':'none'}}
onClick={handleFileSend}>
Отправить
</Button>
</div>
2021-08-17 10:46:28 +05:00
)
}