I have an api that returns this:
it is supposed to be an apk file from the server. How can I achieve to do a react onClick and download the apk file? My current code looks like this, but it isn't working:
export function Download({}) {
const [href, setHref] = useState('')
useEffect(() => {
(async () => {
try {
await userService.downloadMobileApp().then(response => {
setHref(response)});
}
catch (e) {
console.log(e);
if(e.response === undefined){
swal("Error!");
}
if (e.response.status == 401) {
swal(e.response.data.message)
}
else {
swal("Error!");
}
}
})();
}, [])
const downloadApp = async() => {
const downloadLink = document.createElement("a");
const fileName = 'randomAppName.apk';
downloadLink.href = href;
downloadLink.rel='noreferrer'
downloadLink.download = fileName;
console.log("downloadLink: ",downloadLink)
downloadLink.click();
}
return <>
<div >
<Button fillMode="outline" shape="rectangle" size="large"
rounded="large" style={{ backgroundColor: "#c2183b", color: "white",fontWeight: "bold" }}
onClick={()=>downloadApp()}>Download</Button>
</div>
</>
};