diff --git a/static/js/main.js b/static/js/main.js index 5297ee5..205edd2 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -105,23 +105,9 @@ function switchMode(mode) { updateCostPreview(); // 切换模式时同步计费预览 } -function getImageExtension(contentType) { - const type = (contentType || '').split(';')[0].trim().toLowerCase(); - const extensionMap = { - 'image/jpeg': 'jpg', - 'image/jpg': 'jpg', - 'image/png': 'png', - 'image/webp': 'webp', - 'image/gif': 'gif', - 'image/bmp': 'bmp', - 'image/avif': 'avif' - }; - return extensionMap[type] || 'png'; -} - -function buildImageFilename(url, contentType = '') { +function buildImageFilename(url) { downloadFilenameSerial += 1; - const fallbackName = `ai-vision-image-${Date.now()}-${downloadFilenameSerial}.${getImageExtension(contentType)}`; + const fallbackName = `ai-vision-image-${Date.now()}-${downloadFilenameSerial}.png`; try { const parsed = new URL(url, window.location.href); @@ -157,35 +143,22 @@ async function downloadImage(url, options = {}) { if (!url) return false; const silent = options.silent === true; + const cleanupDelay = options.cleanupDelay || 60000; try { if (!silent) showToast('正在准备下载...', 'info'); - const filenameHint = buildImageFilename(url); - const response = await fetch(buildDownloadProxyUrl(url, filenameHint), { - credentials: 'same-origin' - }); + const filename = buildImageFilename(url); + const iframe = document.createElement('iframe'); + iframe.style.display = 'none'; + iframe.src = buildDownloadProxyUrl(url, filename); + document.body.appendChild(iframe); - if (!response.ok) { - let message = `HTTP ${response.status}`; - const responseType = response.headers.get('Content-Type') || ''; - if (responseType.includes('application/json')) { - const data = await response.json(); - message = data.error || message; - } - throw new Error(message); - } + setTimeout(() => { + if (iframe.parentNode) iframe.parentNode.removeChild(iframe); + }, cleanupDelay); - const blob = await response.blob(); - const filename = buildImageFilename(url, blob.type); - const blobUrl = window.URL.createObjectURL(blob); - const a = document.createElement('a'); - a.href = blobUrl; - a.download = filename; - document.body.appendChild(a); - a.click(); - document.body.removeChild(a); - setTimeout(() => window.URL.revokeObjectURL(blobUrl), 1000); + if (!silent) showToast('下载已开始,请留意浏览器下载列表', 'success'); return true; } catch (e) { console.error('下载失败:', e);