| | |
| |
|
| | |
| | function uploadImageAndGetFullUrl(uploadEndpoint, hostUrl, file) { |
| | const formData = new FormData(); |
| | |
| | const loadingElement = document.createElement('div'); |
| | loadingElement.id = 'loading'; |
| | loadingElement.textContent = '图片加载中......'; |
| | document.body.appendChild(loadingElement); |
| |
|
| | |
| | loadingElement.style.position = 'fixed'; |
| | loadingElement.style.top = '50%'; |
| | loadingElement.style.left = '50%'; |
| | loadingElement.style.transform = 'translate(-50%, -50%)'; |
| | loadingElement.style.fontSize = '20px'; |
| | loadingElement.style.color = '#333'; |
| | loadingElement.style.backgroundColor = 'rgba(255, 255, 255, 0.8)'; |
| | loadingElement.style.padding = '10px'; |
| | loadingElement.style.borderRadius = '5px'; |
| | loadingElement.style.zIndex = '1000'; |
| | |
| |
|
| | |
| | function handleCompressFile(file) { |
| | const maxFileSize = 5 * 1024 * 1024; |
| | return new Promise((resolve) => { |
| | if (file.size <= maxFileSize || !file.type.startsWith("image")) { |
| | resolve(file); |
| | } else { |
| | imageCompression(file, { maxSizeMB: 5 }) |
| | .then((compressedFile) => { |
| | resolve(compressedFile); |
| | }) |
| | .catch((error) => { |
| | console.error(">> imageCompression error", error); |
| | resolve(file); |
| | }); |
| | } |
| | }); |
| | } |
| |
|
| | |
| |
|
| | |
| | const uphostUrl = 'https://testupimg.wook.eu.org'; |
| | |
| | return handleCompressFile(file).then(compressedFile => { |
| | formData.append("file", compressedFile); |
| | return fetch(`${uphostUrl}${uploadEndpoint}`, { |
| | method: 'POST', |
| | body: formData |
| | }).finally(() => { |
| | |
| | document.body.removeChild(loadingElement); |
| | }); |
| | }); |
| | } |
| |
|
| | |
| | const hostUrl = 'https://imghost.wook.eu.org'; |
| | const uploadEndpoint = '/upload'; |
| |
|
| | |
| | document.addEventListener('DOMContentLoaded', function() { |
| | const form = document.querySelector('form'); |
| | const urlTextArea = document.querySelector('textarea[name="imageUrls"]'); |
| | const addButton = document.querySelector('input[name="add"]'); |
| | const realFileInput = document.createElement('input'); |
| | realFileInput.type = 'file'; |
| | realFileInput.multiple = true; |
| | realFileInput.style.display = 'none'; |
| |
|
| | |
| | document.body.appendChild(realFileInput); |
| |
|
| | |
| | addButton.addEventListener('click', function(event) { |
| | if (urlTextArea.value.trim() === '') { |
| | event.preventDefault(); |
| | realFileInput.click(); |
| | } |
| | }); |
| |
|
| | |
| | realFileInput.addEventListener('change', function() { |
| | const uploadPromises = Array.from(realFileInput.files).map(file => { |
| | |
| | return uploadImageAndGetFullUrl(uploadEndpoint, hostUrl, file) |
| | .then(response => { |
| | if (!response.ok) { |
| | throw new Error('网络响应不是OK状态'); |
| | } |
| | return response.json(); |
| | }) |
| | .then(data => { |
| | |
| | if (data.error) { |
| | throw new Error(data.error); |
| | } |
| | |
| | const imageUrl = `${hostUrl}${data[0].src}`; |
| | |
| | console.log(imageUrl); |
| | |
| | return imageUrl; |
| | }); |
| | }); |
| |
|
| | Promise.all(uploadPromises) |
| | .then(fullUrls => { |
| | |
| | urlTextArea.value = fullUrls.join('\n'); |
| |
|
| | |
| | |
| | |
| | addButton.click(); |
| | }) |
| | .catch(error => { |
| | console.error('上传失败:', error); |
| | }); |
| | }); |
| | }); |
| |
|
| | function confirmDelete() { |
| | return confirm('确定要删除这张图片吗?'); |
| | } |
| |
|