-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
95 lines (87 loc) · 2.7 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// script.js
const form = document.getElementById('qrForm');
const logoInput = document.getElementById('logo');
const logoPreview = document.getElementById('logoPreview');
const qrCodeImage = document.getElementById('qrCodeImage');
const downloadPNG = document.getElementById('downloadPNG');
const downloadPDF = document.getElementById('downloadPDF');
const downloadSVG = document.getElementById('downloadSVG');
logoInput.addEventListener('change', () => {
const file = logoInput.files[0];
if (file) {
const reader = new FileReader();
reader.onload = () => {
logoPreview.src = reader.result;
logoPreview.style.display = 'inline-block';
};
reader.readAsDataURL(file);
}
});
form.addEventListener('submit', async (event) => {
event.preventDefault();
const url = document.getElementById('url').value;
const logo = logoInput.files[0];
const formData = new FormData();
formData.append('url', url);
formData.append('logo', logo);
try {
const response = await fetch('/generate-qr-code', {
method: 'POST',
body: formData,
});
if (response.ok) {
const blob = await response.blob();
const imageUrl = URL.createObjectURL(blob);
qrCodeImage.src = imageUrl;
} else {
console.error('Error generating QR code:', response.statusText);
}
} catch (error) {
console.error('Error:', error);
}
});
downloadPNG.addEventListener('click', async () => {
try {
const response = await fetch('/download-qr-code?format=png');
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'qr-code.png';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
} catch (error) {
console.error('Error:', error);
}
});
downloadPDF.addEventListener('click', async () => {
try {
const response = await fetch('/download-qr-code?format=pdf');
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'qr-code.pdf';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
} catch (error) {
console.error('Error:', error);
}
});
downloadSVG.addEventListener('click', async () => {
try {
const response = await fetch('/download-qr-code?format=svg');
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'qr-code.svg';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
} catch (error) {
console.error('Error:', error);
}
});