-
Notifications
You must be signed in to change notification settings - Fork 2
/
json_download.html
92 lines (86 loc) · 2.57 KB
/
json_download.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Auto Download JSON</title>
<style>
#progress-container {
width: 100%;
background-color: #f3f3f3;
margin-top: 20px;
}
#progress-bar {
width: 0%;
height: 30px;
background-color: #4caf50;
text-align: center;
line-height: 30px;
color: black;
}
</style>
</head>
<body>
<div id="progress-container">
다운로드중...
<div id="progress-bar">0%</div>
</div>
<script>
function getQueryParameter(name) {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(name);
}
async function downloadFile(url, filename) {
const response = await fetch(url);
const contentLength = response.headers.get('content-length');
const total = parseInt(contentLength, 10);
let loaded = 0;
const reader = response.body.getReader();
const stream = new ReadableStream({
start(controller) {
function push() {
reader.read().then(({ done, value }) => {
if (done) {
controller.close();
return;
}
loaded += value.byteLength;
const progress = (loaded / total) * 100;
updateProgressBar(progress);
controller.enqueue(value);
push();
}).catch(error => {
console.error('Error reading stream:', error);
controller.error(error);
});
}
push();
}
});
const newResponse = new Response(stream);
const blob = await newResponse.blob();
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
function updateProgressBar(progress) {
const progressBar = document.getElementById('progress-bar');
progress = Math.min(progress, 100);
progressBar.style.width = progress + '%';
progressBar.textContent = Math.floor(progress) + '%';
}
document.addEventListener('DOMContentLoaded', () => {
const jsonUrl = getQueryParameter('jsonUrl');
if (jsonUrl) {
const filename = jsonUrl.split('/').pop() || 'download.json';
downloadFile(jsonUrl, filename);
} else {
console.error('No jsonUrl query parameter provided.');
}
});
</script>
</body>
</html>