Update index.js
Browse files
index.js
CHANGED
@@ -30,19 +30,35 @@ app.get('/', (req, res) => {
|
|
30 |
</form>
|
31 |
<script>
|
32 |
document.getElementById('uploadForm').addEventListener('submit', async (event) => {
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
</script>
|
47 |
</body>
|
48 |
</html>
|
|
|
30 |
</form>
|
31 |
<script>
|
32 |
document.getElementById('uploadForm').addEventListener('submit', async (event) => {
|
33 |
+
event.preventDefault();
|
34 |
+
const formData = new FormData(event.target);
|
35 |
+
const response = await fetch(event.target.action, {
|
36 |
+
method: 'POST',
|
37 |
+
body: formData
|
38 |
+
});
|
39 |
+
|
40 |
+
if (response.ok) {
|
41 |
+
// 假设服务器返回的是一个可下载的文件
|
42 |
+
response.blob().then(blob => {
|
43 |
+
// 创建一个链接元素
|
44 |
+
const url = window.URL.createObjectURL(blob);
|
45 |
+
const a = document.createElement('a');
|
46 |
+
a.style.display = 'none';
|
47 |
+
a.href = url;
|
48 |
+
// 从Content-Disposition头中提取文件名
|
49 |
+
const contentDisposition = response.headers.get('Content-Disposition');
|
50 |
+
const fileName = contentDisposition.match(/filename="(.+)"/)[1];
|
51 |
+
a.download = fileName;
|
52 |
+
document.body.appendChild(a);
|
53 |
+
a.click();
|
54 |
+
window.URL.revokeObjectURL(url);
|
55 |
+
alert('File downloaded successfully!');
|
56 |
+
});
|
57 |
+
} else {
|
58 |
+
alert('Failed to upload file.');
|
59 |
+
}
|
60 |
+
});
|
61 |
+
|
62 |
</script>
|
63 |
</body>
|
64 |
</html>
|