From b6bac2389feb069306537d7ba1cb8a6e1862776f Mon Sep 17 00:00:00 2001 From: elmgates Date: Tue, 27 May 2025 13:23:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20file-share-frontend/script?= =?UTF-8?q?.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- file-share-frontend/script.js | 174 +++++++++++++++++----------------- 1 file changed, 87 insertions(+), 87 deletions(-) diff --git a/file-share-frontend/script.js b/file-share-frontend/script.js index ebe2824..0cb8239 100755 --- a/file-share-frontend/script.js +++ b/file-share-frontend/script.js @@ -1,88 +1,88 @@ -document.addEventListener('DOMContentLoaded', function() { - const uploadForm = document.getElementById('uploadForm'); - const downloadForm = document.getElementById('downloadForm'); - const uploadResult = document.getElementById('uploadResult'); - const fileLink = document.getElementById('fileLink'); - const copyLinkBtn = document.getElementById('copyLinkBtn'); - - // 设置最小过期时间为当前时间 - const expiresInput = document.getElementById('expiresInput'); - const now = new Date(); - const localDatetime = new Date(now.getTime() - now.getTimezoneOffset() * 60000) - .toISOString() - .slice(0, 16); - expiresInput.min = localDatetime; - - // 文件上传处理 - uploadForm.addEventListener('submit', async function(e) { - e.preventDefault(); - - const formData = new FormData(uploadForm); - const fileInput = document.getElementById('fileInput'); - - if (!fileInput.files[0]) { - alert('请选择要上传的文件'); - return; - } - - try { - const response = await fetch('https://share.ssplus.cn/upload', { - method: 'POST', - body: formData - }); - - const result = await response.json(); - - if (result.success) { - // 显示上传结果 - fileLink.href = result.url; - fileLink.textContent = result.url; - uploadResult.classList.remove('hidden'); - - // 清空表单 - uploadForm.reset(); - } else { - alert(`上传失败: ${result.error || '未知错误'}`); - } - } catch (error) { - alert(`上传出错: ${error.message}`); - } - }); - - // 复制链接功能 - copyLinkBtn.addEventListener('click', function() { - navigator.clipboard.writeText(fileLink.href) - .then(() => { - const originalText = copyLinkBtn.textContent; - copyLinkBtn.textContent = '已复制!'; - setTimeout(() => { - copyLinkBtn.textContent = originalText; - }, 2000); - }) - .catch(err => { - alert('复制失败: ' + err); - }); - }); - - // 文件下载处理 - downloadForm.addEventListener('submit', function(e) { - e.preventDefault(); - - const fileId = document.getElementById('fileIdInput').value.trim(); - const password = document.getElementById('downloadPasswordInput').value; - - if (!fileId) { - alert('请输入文件ID'); - return; - } - - // 构建下载URL - let downloadUrl = `https://share.ssplus.cn/file/${fileId}`; - if (password) { - downloadUrl += `?password=${encodeURIComponent(password)}`; - } - - // 打开下载链接 - window.open(downloadUrl, '_blank'); - }); +document.addEventListener('DOMContentLoaded', function() { + const uploadForm = document.getElementById('uploadForm'); + const downloadForm = document.getElementById('downloadForm'); + const uploadResult = document.getElementById('uploadResult'); + const fileLink = document.getElementById('fileLink'); + const copyLinkBtn = document.getElementById('copyLinkBtn'); + + // 设置最小过期时间为当前时间 + const expiresInput = document.getElementById('expiresInput'); + const now = new Date(); + const localDatetime = new Date(now.getTime() - now.getTimezoneOffset() * 60000) + .toISOString() + .slice(0, 16); + expiresInput.min = localDatetime; + + // 文件上传处理 + uploadForm.addEventListener('submit', async function(e) { + e.preventDefault(); + + const formData = new FormData(uploadForm); + const fileInput = document.getElementById('fileInput'); + + if (!fileInput.files[0]) { + alert('请选择要上传的文件'); + return; + } + + try { + const response = await fetch('<你的后端地址>/upload', { + method: 'POST', + body: formData + }); + + const result = await response.json(); + + if (result.success) { + // 显示上传结果 + fileLink.href = result.url; + fileLink.textContent = result.url; + uploadResult.classList.remove('hidden'); + + // 清空表单 + uploadForm.reset(); + } else { + alert(`上传失败: ${result.error || '未知错误'}`); + } + } catch (error) { + alert(`上传出错: ${error.message}`); + } + }); + + // 复制链接功能 + copyLinkBtn.addEventListener('click', function() { + navigator.clipboard.writeText(fileLink.href) + .then(() => { + const originalText = copyLinkBtn.textContent; + copyLinkBtn.textContent = '已复制!'; + setTimeout(() => { + copyLinkBtn.textContent = originalText; + }, 2000); + }) + .catch(err => { + alert('复制失败: ' + err); + }); + }); + + // 文件下载处理 + downloadForm.addEventListener('submit', function(e) { + e.preventDefault(); + + const fileId = document.getElementById('fileIdInput').value.trim(); + const password = document.getElementById('downloadPasswordInput').value; + + if (!fileId) { + alert('请输入文件ID'); + return; + } + + // 构建下载URL + let downloadUrl = `<你的后端地址>/file/${fileId}`; + if (password) { + downloadUrl += `?password=${encodeURIComponent(password)}`; + } + + // 打开下载链接 + window.open(downloadUrl, '_blank'); + }); }); \ No newline at end of file