forked from ddrilling/asb_cloud_front
Improve fetch error handling
This commit is contained in:
parent
9360724ad2
commit
0aacc05dac
7
src/components/ErrorFetch.js
Normal file
7
src/components/ErrorFetch.js
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
export class ErrorFetch extends Error {
|
||||||
|
constructor(status, message) {
|
||||||
|
super(message);
|
||||||
|
this.name = "ErrorFetch"
|
||||||
|
this.status = status
|
||||||
|
}
|
||||||
|
}
|
@ -2,22 +2,38 @@ import { Upload, Button } from 'antd'
|
|||||||
import { UploadOutlined } from '@ant-design/icons'
|
import { UploadOutlined } from '@ant-design/icons'
|
||||||
import { useState } from 'react'
|
import { useState } from 'react'
|
||||||
import { upload } from './factory'
|
import { upload } from './factory'
|
||||||
|
import { ErrorFetch } from './ErrorFetch'
|
||||||
|
|
||||||
export default function UploadForm({url, accept, onUploadStart, onUploadComplete, onUploadError}) {
|
export const UploadForm = ({url, accept, style, formData, onUploadStart, onUploadSuccess, onUploadComplete, onUploadError}) => {
|
||||||
const [fileList, setfileList] = useState([])
|
const [fileList, setfileList] = useState([])
|
||||||
|
|
||||||
const handleFileSend = async (values) => {
|
const handleFileSend = async () => {
|
||||||
if(onUploadStart)
|
if(onUploadStart)
|
||||||
onUploadStart()
|
onUploadStart()
|
||||||
try {
|
try {
|
||||||
const formData = new FormData()
|
const formDataLocal = new FormData()
|
||||||
fileList.forEach((val) => {
|
fileList.forEach((val) => {
|
||||||
formData.append("files", val.originFileObj);
|
formDataLocal.append("files", val.originFileObj)
|
||||||
});
|
})
|
||||||
await upload(url, formData)
|
|
||||||
|
if(formData)
|
||||||
|
for(var propName in formData)
|
||||||
|
formDataLocal.append(propName, formData[propName])
|
||||||
|
|
||||||
|
const response = await upload(url, formDataLocal)
|
||||||
|
if(!response.ok)
|
||||||
|
{
|
||||||
|
const errorText = await response.text()
|
||||||
|
const error = new ErrorFetch(response.status, errorText)
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
if(onUploadSuccess)
|
||||||
|
onUploadSuccess()
|
||||||
|
}
|
||||||
} catch(error) {
|
} catch(error) {
|
||||||
if(onUploadError)
|
if(onUploadError)
|
||||||
onUploadError(error)
|
onUploadError(error)
|
||||||
} finally {
|
} finally {
|
||||||
setfileList([])
|
setfileList([])
|
||||||
if(onUploadComplete)
|
if(onUploadComplete)
|
||||||
@ -27,7 +43,7 @@ export default function UploadForm({url, accept, onUploadStart, onUploadComplete
|
|||||||
|
|
||||||
const isSendButtonEnabled = fileList.length > 0
|
const isSendButtonEnabled = fileList.length > 0
|
||||||
return(
|
return(
|
||||||
<div style={{display: 'flex'}}>
|
<div style={{display: 'flex', ...style}}>
|
||||||
<Upload
|
<Upload
|
||||||
name ="file"
|
name ="file"
|
||||||
fileList={fileList}
|
fileList={fileList}
|
||||||
|
@ -81,13 +81,14 @@ export const download = async (url:string, fileName?:string) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const upload = async (url:string, formData: FormData) => {
|
export const upload = async (url:string, formData: FormData) => {
|
||||||
await fetch(url, {
|
let response = await fetch(url, {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: 'Bearer ' + localStorage['token']
|
Authorization: 'Bearer ' + localStorage['token']
|
||||||
},
|
},
|
||||||
method: 'Post',
|
method: 'Post',
|
||||||
body: formData,
|
body: formData,
|
||||||
})
|
})
|
||||||
|
return response
|
||||||
}
|
}
|
||||||
|
|
||||||
export const downloadFile = async (fileInfo: FileInfoDto) => {
|
export const downloadFile = async (fileInfo: FileInfoDto) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user