-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
35 lines (32 loc) · 1.19 KB
/
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
27
28
29
30
31
32
33
34
35
const fileInput = document.querySelector("input");
const downloadBtn = document.querySelector("#download");
const previewBtn = document.querySelector("#preview");
const imgTag = document.querySelector("#image")
previewBtn.addEventListener("click", e => {
e.preventDefault();
imgTag.style.display = "block";
imgTag.src = fileInput.value;
previewBtn.style.display = "none";
downloadBtn.style.display = "block";
});
downloadBtn.addEventListener("click", e => {
e.preventDefault();
downloadBtn.innerText = "Downloading file...";
fetchFile(fileInput.value);
});
function fetchFile(url) {
fetch(url).then(res => res.blob()).then(file => {
let tempUrl = URL.createObjectURL(file);
const aTag = document.createElement("a");
aTag.href = tempUrl;
aTag.download = "AT Dilshan - " + url.replace(/^.*[\\\/]/, '');
document.body.appendChild(aTag);
aTag.click();
previewBtn.style.display = 'block';
downloadBtn.style.display = 'none';
imgTag.style.display = "none";
downloadBtn.innerText = "Download File";
URL.revokeObjectURL(tempUrl);
aTag.removeAttribute();
})
}