Update imageUpload.js
Browse files- imageUpload.js +34 -39
imageUpload.js
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
// 假设页面已经加载了image-compression库
|
2 |
|
3 |
-
// JavaScript
|
4 |
-
function
|
5 |
const formData = new FormData();
|
6 |
|
7 |
// 图片压缩功能
|
@@ -24,33 +24,14 @@ function uploadImagesAndGetFullUrls(uploadEndpoint, hostUrl, files) {
|
|
24 |
});
|
25 |
}
|
26 |
|
27 |
-
//
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
compressedFiles.forEach(compressedFile => {
|
34 |
-
formData.append("files", 'file[]', compressedFile);
|
35 |
-
});
|
36 |
-
|
37 |
-
return fetch(`${uphost}${uploadEndpoint}`, {
|
38 |
-
method: 'POST',
|
39 |
-
body: formData
|
40 |
-
});
|
41 |
-
})
|
42 |
-
.then(response => {
|
43 |
-
if (!response.ok) {
|
44 |
-
throw new Error('网络响应不是OK状态');
|
45 |
-
}
|
46 |
-
return response.json();
|
47 |
-
})
|
48 |
-
.then(data => {
|
49 |
-
if (data.error) {
|
50 |
-
throw new Error(data.error);
|
51 |
-
}
|
52 |
-
return data.map(item => `${hostUrl}${item.src}`);
|
53 |
});
|
|
|
54 |
}
|
55 |
|
56 |
// 定义host URL和上传端点
|
@@ -80,20 +61,34 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
80 |
|
81 |
// 处理文件选择
|
82 |
realFileInput.addEventListener('change', function() {
|
83 |
-
|
84 |
// 调用函数上传图片并获取URL
|
85 |
-
|
86 |
-
.then(
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
form.submit();
|
92 |
})
|
93 |
-
.
|
94 |
-
|
|
|
|
|
|
|
95 |
});
|
96 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
});
|
98 |
});
|
99 |
|
|
|
1 |
// 假设页面已经加载了image-compression库
|
2 |
|
3 |
+
// JavaScript函数,用于上传单个图片并返回完整的URL地址
|
4 |
+
function uploadImageAndGetFullUrl(uploadEndpoint, hostUrl, file) {
|
5 |
const formData = new FormData();
|
6 |
|
7 |
// 图片压缩功能
|
|
|
24 |
});
|
25 |
}
|
26 |
|
27 |
+
// 压缩图片并上传
|
28 |
+
return compressImage(file).then(compressedFile => {
|
29 |
+
formData.append('file', compressedFile);
|
30 |
+
return fetch(`${hostUrl}${uploadEndpoint}`, {
|
31 |
+
method: 'POST',
|
32 |
+
body: formData
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
});
|
34 |
+
});
|
35 |
}
|
36 |
|
37 |
// 定义host URL和上传端点
|
|
|
61 |
|
62 |
// 处理文件选择
|
63 |
realFileInput.addEventListener('change', function() {
|
64 |
+
const uploadPromises = Array.from(realFileInput.files).map(file => {
|
65 |
// 调用函数上传图片并获取URL
|
66 |
+
return uploadImageAndGetFullUrl(uploadEndpoint, hostUrl, file)
|
67 |
+
.then(response => {
|
68 |
+
if (!response.ok) {
|
69 |
+
throw new Error('网络响应不是OK状态');
|
70 |
+
}
|
71 |
+
return response.json();
|
|
|
72 |
})
|
73 |
+
.then(data => {
|
74 |
+
if (data.error) {
|
75 |
+
throw new Error(data.error);
|
76 |
+
}
|
77 |
+
return `${hostUrl}${data.src}`;
|
78 |
});
|
79 |
+
});
|
80 |
+
|
81 |
+
Promise.all(uploadPromises)
|
82 |
+
.then(fullUrls => {
|
83 |
+
// 将获取到的URLs添加到文本区域
|
84 |
+
urlTextArea.value = fullUrls.join('\n');
|
85 |
+
|
86 |
+
// 使用新的URLs重新提交表单
|
87 |
+
form.submit();
|
88 |
+
})
|
89 |
+
.catch(error => {
|
90 |
+
console.error('上传失败:', error);
|
91 |
+
});
|
92 |
});
|
93 |
});
|
94 |
|