Create 240429imgupload.js
Browse files- 240429imgupload.js +106 -0
240429imgupload.js
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// 假设页面已经加载了image-compression库
|
2 |
+
|
3 |
+
// JavaScript函数,用于上传单个图片并返回完整的URL地址
|
4 |
+
function uploadImageAndGetFullUrl(uploadEndpoint, hostUrl, file) {
|
5 |
+
const formData = new FormData();
|
6 |
+
|
7 |
+
// 图片压缩功能
|
8 |
+
function handleCompressFile(file) {
|
9 |
+
const maxFileSize = 5 * 1024 * 1024; // 5MB
|
10 |
+
return new Promise((resolve) => {
|
11 |
+
if (file.size <= maxFileSize || !file.type.startsWith("image")) {
|
12 |
+
resolve(file);
|
13 |
+
} else {
|
14 |
+
imageCompression(file, { maxSizeMB: 5 })
|
15 |
+
.then((compressedFile) => {
|
16 |
+
resolve(compressedFile);
|
17 |
+
})
|
18 |
+
.catch((error) => {
|
19 |
+
console.error(">> imageCompression error", error);
|
20 |
+
resolve(file);
|
21 |
+
});
|
22 |
+
}
|
23 |
+
});
|
24 |
+
}
|
25 |
+
|
26 |
+
|
27 |
+
|
28 |
+
// 压缩图片并上传
|
29 |
+
const uphostUrl = 'https://testupimg.wook.eu.org';
|
30 |
+
//'https://testupimg.sokwith.workers.dev'
|
31 |
+
return handleCompressFile(file).then(compressedFile => {
|
32 |
+
formData.append("file", compressedFile);
|
33 |
+
return fetch(`${uphostUrl}${uploadEndpoint}`, {
|
34 |
+
method: 'POST',
|
35 |
+
body: formData
|
36 |
+
});
|
37 |
+
});
|
38 |
+
}
|
39 |
+
|
40 |
+
// 定义host URL和上传端点
|
41 |
+
const hostUrl = 'https://imghost.wook.eu.org';
|
42 |
+
const uploadEndpoint = '/upload';
|
43 |
+
|
44 |
+
// 在文档加载完成后添加事件监听器
|
45 |
+
document.addEventListener('DOMContentLoaded', function() {
|
46 |
+
const form = document.querySelector('form');
|
47 |
+
const urlTextArea = document.querySelector('textarea[name="imageUrls"]');
|
48 |
+
const addButton = document.querySelector('input[name="add"]');
|
49 |
+
const realFileInput = document.createElement('input');
|
50 |
+
realFileInput.type = 'file';
|
51 |
+
realFileInput.multiple = true;
|
52 |
+
realFileInput.style.display = 'none';
|
53 |
+
|
54 |
+
// 将文件输入元素添加到DOM中,但不显示
|
55 |
+
document.body.appendChild(realFileInput);
|
56 |
+
|
57 |
+
// 当点击“批量添加图片”按钮时触发文件输入元素的点击事件
|
58 |
+
addButton.addEventListener('click', function(event) {
|
59 |
+
if (urlTextArea.value.trim() === '') {
|
60 |
+
event.preventDefault();
|
61 |
+
realFileInput.click();
|
62 |
+
}
|
63 |
+
});
|
64 |
+
|
65 |
+
// 处理文件选择
|
66 |
+
realFileInput.addEventListener('change', function() {
|
67 |
+
const uploadPromises = Array.from(realFileInput.files).map(file => {
|
68 |
+
// 调用函数上传图片并获取URL
|
69 |
+
return uploadImageAndGetFullUrl(uploadEndpoint, hostUrl, file)
|
70 |
+
.then(response => {
|
71 |
+
if (!response.ok) {
|
72 |
+
throw new Error('网络响应不是OK状态');
|
73 |
+
}
|
74 |
+
return response.json();
|
75 |
+
})
|
76 |
+
.then(data => {
|
77 |
+
// console.log(data);
|
78 |
+
if (data.error) {
|
79 |
+
throw new Error(data.error);
|
80 |
+
}
|
81 |
+
// 创建一个变量来存储完整的图片URL
|
82 |
+
const imageUrl = `${hostUrl}${data[0].src}`;
|
83 |
+
// 在控制台输出完整的图片URL
|
84 |
+
console.log(imageUrl);
|
85 |
+
// 返回完整的图片URL
|
86 |
+
return imageUrl;
|
87 |
+
});
|
88 |
+
});
|
89 |
+
|
90 |
+
Promise.all(uploadPromises)
|
91 |
+
.then(fullUrls => {
|
92 |
+
// 将获取到的URLs添加到文本区域
|
93 |
+
urlTextArea.value = fullUrls.join('\n');
|
94 |
+
|
95 |
+
// 使用新的URLs重新提交表单
|
96 |
+
//form.submit();
|
97 |
+
})
|
98 |
+
.catch(error => {
|
99 |
+
console.error('上传失败:', error);
|
100 |
+
});
|
101 |
+
});
|
102 |
+
});
|
103 |
+
|
104 |
+
function confirmDelete() {
|
105 |
+
return confirm('确定要删除这张图片吗?');
|
106 |
+
}
|