88 lines
3.0 KiB
JavaScript
Executable File
88 lines
3.0 KiB
JavaScript
Executable File
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');
|
|
});
|
|
}); |