-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
26 lines (25 loc) · 836 Bytes
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const fileInput=document.querySelector("input");
downloadBtn=document.querySelector("button");
downloadBtn.addEventListener("click",e =>{
e.preventDefault();
downloadBtn.innerText="Downloading file...";
fetchFile(fileInput.value);
});
function fetchFile(url){
// fetching file and returning response as blob
fetch(url).then(res=>res.blob()).then(file=>{
// url.createobjURL creates a url of passed object
let tempUrl=URL.createObjectURL(file);
let aTag=document.createElement("a")
aTag.href=tempUrl;
aTag.download=url.replace(/^.*[\\\/]/, '');
document.body.appendChild(aTag)
aTag.click();
aTag.remove();
URL.revokeObjectURL(tempUrl);
downloadBtn.innerText="Download file...";
}).catch(()=>{
downloadBtn.innerText="Download file...";
alert("failed to download file!")
})
}