forked from ddrilling/asb_cloud_front
Add download function
This commit is contained in:
parent
28ad2f63f1
commit
5f6a4724cc
@ -69,7 +69,7 @@ export const makePaginationObject = (paginationContainer:PaginationContainer, ..
|
||||
}
|
||||
}
|
||||
|
||||
export const updateFromWebApiWrapperAsync = async (funcAsync: asyncFunction, setShowLoader: Dispatch<SetStateAction<boolean>>, errorNotifyText: string) => {
|
||||
export const updateFromWebApiWrapperAsync = async (funcAsync: asyncFunction, setShowLoader: Dispatch<SetStateAction<boolean>>, errorNotifyText: string) => {
|
||||
if(setShowLoader)
|
||||
setShowLoader(true)
|
||||
try{
|
||||
@ -83,4 +83,45 @@ export const updateFromWebApiWrapperAsync = async (funcAsync: asyncFunction, set
|
||||
if(setShowLoader)
|
||||
setShowLoader(false)
|
||||
}
|
||||
}
|
||||
|
||||
export const download = async (url:string, fileName?:string) => {
|
||||
const response = await fetch(url, {
|
||||
headers: {
|
||||
Authorization: 'Bearer ' + localStorage['token']
|
||||
},
|
||||
method: 'Get'
|
||||
})
|
||||
const requestFileName = decodeURI(fileName
|
||||
??response.headers
|
||||
.get('content-disposition')
|
||||
?.split(';')
|
||||
.splice(-1)[0]
|
||||
.split("'")
|
||||
.splice(-1)[0]
|
||||
?? url.replace('\\','/')
|
||||
.split('/')
|
||||
.splice(-1)[0]
|
||||
?? 'file')
|
||||
const blob = await response.blob()
|
||||
const reader = new FileReader();
|
||||
reader.readAsDataURL(blob);
|
||||
reader.onload = function (e) {
|
||||
let a = document.createElement('a');
|
||||
a.href = (e.target?.result?.toString() ?? '');
|
||||
a.download = requestFileName;
|
||||
document.body.appendChild(a); // we need to append the element to the dom -> otherwise it will not work in firefox
|
||||
a.click();
|
||||
a.remove();
|
||||
};
|
||||
}
|
||||
|
||||
export const upload = async (url:string, formData: FormData) => {
|
||||
await fetch(url, {
|
||||
headers: {
|
||||
Authorization: 'Bearer ' + localStorage['token']
|
||||
},
|
||||
method: 'Post',
|
||||
body: formData,
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user