-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
45 lines (37 loc) · 1.47 KB
/
script.js
File metadata and controls
45 lines (37 loc) · 1.47 KB
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
document.getElementById('convertButton').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
const format = document.getElementById('format').value;
const status = document.getElementById('status');
const downloadLink = document.getElementById('downloadLink');
// Reset previous status and download link
status.textContent = '';
downloadLink.style.display = 'none';
if (fileInput.files.length === 0) {
status.textContent = 'Please select a file.';
return;
}
const file = fileInput.files[0];
const formData = new FormData();
formData.append('file', file);
formData.append('format', format);
status.textContent = 'Uploading and converting...';
try {
const response = await fetch('https://file-compressor-two.vercel.app/', {
method: 'POST',
body: formData
});
if (!response.ok) {
throw new Error('Conversion failed');
}
const blob = await response.blob();
const url = URL.createObjectURL(blob);
downloadLink.href = url;
downloadLink.download = `converted.${format}`;
downloadLink.textContent = `Download ${format.toUpperCase()} File`;
downloadLink.style.display = 'block';
status.textContent = 'Conversion successful!';
} catch (error) {
console.error('Error:', error);
status.textContent = 'An error occurred. Please try again.';
}
});