forked from ddrilling/asb_cloud_front
Add download function
This commit is contained in:
parent
28ad2f63f1
commit
5f6a4724cc
@ -84,3 +84,44 @@ export const updateFromWebApiWrapperAsync = async (funcAsync: asyncFunction, set
|
|||||||
setShowLoader(false)
|
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