forked from Arkapro1/fileDownloaderWebProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
25 lines (25 loc) · 835 Bytes
/
script.js
File metadata and controls
25 lines (25 loc) · 835 Bytes
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
const link=document.querySelector("input");
const btn= document.querySelector("button");
btn.addEventListener("click",()=>
{ btn.innerHTML=`Downloading⚡⬇file......`;
fetchFile(link.value);
}
);
function fetchFile(url){
fetch(url).then(res=>res.blob()).then(file=>{
let tempurl= URL.createObjectURL(file);
// console.log(tempurl);
let aTag=document.createElement("a");
aTag.href=tempurl;
// aTag.download=url.replace(/^.*[\\\/]/, '');
aTag.download="fileName";
document.body.appendChild(aTag);
aTag.click();// to donwload;
aTag.remove();// to remove after dwnld;
URL.revokeObjectURL(tempurl);
btn.innerHTML=`Download file`;
}).catch(()=>{
alert("file source is restricted");
btn.innerHTML=`Download file`;
})
}