-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
67 lines (47 loc) · 1.95 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const inputBox = document.querySelector("#input");
const qrImage = document.querySelector("#qrimage");
const imageBox = document.querySelector("#image-box");
function generateQR(){
if(inputBox.value.length > 0){
qrImage.src = "https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=" + inputBox.value;
imageBox.classList.add("show-img");
document.querySelector("#download").style.display = "block"
}
else{
inputBox.classList.add("error");
setTimeout(()=>{
inputBox.classList.remove("error")
},1000)
}
}
//Download Any File
function downloadFile(){
if(imageBox.classList.contains("show-img")){
// if(inputBox.value.length > 0){
document.querySelector("#download").innerHTML = "Downloading....";
let url = "https://api.qrserver.com/v1/create-qr-code/?size=250x250&data=" + inputBox.value;
//Fetching file & returing respnse as blob
fetch(url).then(res=>
res.blob())
.then(file=>{
//creates a new url of passed object
let tempUrl = URL.createObjectURL(file);
let aTag = document.createElement("a");
//passing the tempUrl in <a> tag
aTag.href = tempUrl;
aTag.download = "QR" //file name
//adding the <a> tag inside the body
document.body.appendChild(aTag)
//clicking <a> tag so the file download
aTag.click();
//removing <a> tag once the file downloaded
aTag.remove();
//removing the tempUrl from the document
URL.revokeObjectURL(tempUrl);
document.querySelector("#download").innerHTML = "Download QR";
})
.catch(()=>{
alert("Unable to Download...")
})
}
}