AI 生成: 一个基于浏览器网址的跨设备文件快传工具,支持拖拽上传、二维码扫描、实时传输进度显示。

This commit is contained in:
eeuse
2026-09-28 22:46:55 +08:00
commit 7b09d4b79f
6 changed files with 745 additions and 0 deletions
+189
View File
@@ -0,0 +1,189 @@
// ========== 初始化二维码 ==========
document.addEventListener('DOMContentLoaded', function () {
new QRCode(document.getElementById('qrcode'), {
text: SERVER_URL,
width: 140,
height: 140,
colorDark: '#333333',
colorLight: '#ffffff',
correctLevel: QRCode.CorrectLevel.M
});
loadFiles();
});
// ========== 复制链接 ==========
function copyUrl() {
const url = document.getElementById('urlValue').textContent;
navigator.clipboard.writeText(url).then(() => {
const btn = document.querySelector('.btn-copy');
const original = btn.textContent;
btn.textContent = '已复制 ✓';
setTimeout(() => btn.textContent = original, 1500);
}).catch(() => {
// 兼容旧浏览器
const input = document.createElement('input');
input.value = url;
document.body.appendChild(input);
input.select();
document.execCommand('copy');
document.body.removeChild(input);
});
}
// ========== 上传相关 ==========
const dropZone = document.getElementById('dropZone');
const fileInput = document.getElementById('fileInput');
const progressContainer = document.getElementById('progressContainer');
const progressFill = document.getElementById('progressFill');
const progressName = document.getElementById('progressName');
const progressPercent = document.getElementById('progressPercent');
// 点击触发文件选择
dropZone.addEventListener('click', () => fileInput.click());
// 文件选择
fileInput.addEventListener('change', (e) => {
if (e.target.files.length) {
uploadFiles(Array.from(e.target.files));
fileInput.value = '';
}
});
// 拖拽事件
['dragenter', 'dragover'].forEach(evt => {
dropZone.addEventListener(evt, (e) => {
e.preventDefault();
e.stopPropagation();
dropZone.classList.add('dragover');
});
});
['dragleave', 'drop'].forEach(evt => {
dropZone.addEventListener(evt, (e) => {
e.preventDefault();
e.stopPropagation();
dropZone.classList.remove('dragover');
});
});
dropZone.addEventListener('drop', (e) => {
const files = Array.from(e.dataTransfer.files);
if (files.length) uploadFiles(files);
});
// 批量上传
function uploadFiles(files) {
let index = 0;
const total = files.length;
function next() {
if (index >= total) {
progressContainer.style.display = 'none';
loadFiles();
return;
}
const file = files[index];
index++;
uploadSingle(file, () => next());
}
next();
}
// 单个文件上传
function uploadSingle(file, callback) {
const formData = new FormData();
formData.append('file', file);
const xhr = new XMLHttpRequest();
progressContainer.style.display = 'block';
progressName.textContent = file.name;
progressFill.style.width = '0%';
progressPercent.textContent = '0%';
xhr.upload.addEventListener('progress', (e) => {
if (e.lengthComputable) {
const percent = Math.round((e.loaded / e.total) * 100);
progressFill.style.width = percent + '%';
progressPercent.textContent = percent + '%';
}
});
xhr.addEventListener('load', () => {
progressFill.style.width = '100%';
progressPercent.textContent = '100%';
setTimeout(callback, 300);
});
xhr.addEventListener('error', () => {
alert('上传失败:' + file.name);
callback();
});
xhr.open('POST', '/upload');
xhr.send(formData);
}
// ========== 文件列表 ==========
function loadFiles() {
fetch('/files')
.then(res => res.json())
.then(files => renderFiles(files))
.catch(err => console.error('加载文件失败', err));
}
function renderFiles(files) {
const list = document.getElementById('fileList');
if (!files.length) {
list.innerHTML = '<li class="empty">暂无文件</li>';
return;
}
list.innerHTML = files.map(f => `
<li>
<div class="file-info">
<span class="file-icon">${getIcon(f.name)}</span>
<div class="file-meta">
<div class="file-name" title="${escapeHtml(f.name)}">${escapeHtml(f.name)}</div>
<div class="file-size">${f.size}</div>
</div>
</div>
<div class="file-actions">
<a class="btn-download" href="/download/${encodeURIComponent(f.name)}">下载</a>
<button class="btn-delete" onclick="deleteFile('${escapeJs(f.name)}')">删除</button>
</div>
</li>
`).join('');
}
function deleteFile(name) {
if (!confirm('确定删除 ' + name + ' ?')) return;
fetch('/delete/' + encodeURIComponent(name), { method: 'DELETE' })
.then(res => res.json())
.then(() => loadFiles())
.catch(err => alert('删除失败'));
}
// ========== 工具函数 ==========
function getIcon(name) {
const ext = name.split('.').pop().toLowerCase();
const map = {
jpg: '🖼️', jpeg: '🖼️', png: '🖼️', gif: '🖼️', webp: '🖼️', svg: '🖼️',
mp4: '🎬', mov: '🎬', avi: '🎬', mkv: '🎬',
mp3: '🎵', wav: '🎵', flac: '🎵',
pdf: '📕', doc: '📘', docx: '📘', xls: '📗', xlsx: '📗', ppt: '📙', pptx: '📙',
zip: '🗜️', rar: '🗜️', '7z': '🗜️', tar: '🗜️', gz: '🗜️',
txt: '📄', md: '📝', json: '📋', xml: '📋',
exe: '⚙️', apk: '📱', dmg: '💿', iso: '💿'
};
return map[ext] || '📄';
}
function escapeHtml(str) {
return String(str).replace(/[&<>"']/g, s => ({
'&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;'
}[s]));
}
function escapeJs(str) {
return String(str).replace(/\\/g, '\\\\').replace(/'/g, "\\'").replace(/"/g, '\\"');
}
+291
View File
@@ -0,0 +1,291 @@
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
color: #333;
}
.container {
max-width: 720px;
margin: 0 auto;
}
header {
text-align: center;
color: #fff;
margin-bottom: 30px;
}
header h1 {
font-size: 2.2rem;
margin-bottom: 8px;
}
.subtitle {
opacity: 0.9;
font-size: 1rem;
}
section {
background: #fff;
border-radius: 16px;
padding: 24px;
margin-bottom: 20px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}
/* 二维码区域 */
.qr-section {
display: flex;
align-items: center;
gap: 24px;
flex-wrap: wrap;
justify-content: center;
}
.qr-box {
text-align: center;
}
#qrcode {
display: inline-block;
padding: 8px;
background: #fff;
border-radius: 8px;
}
#qrcode img, #qrcode canvas {
display: block;
}
.qr-tip {
margin-top: 8px;
color: #666;
font-size: 0.85rem;
}
.url-box {
flex: 1;
min-width: 240px;
}
.url-label {
color: #666;
font-size: 0.85rem;
margin-bottom: 6px;
}
.url-value {
background: #f5f5f7;
padding: 12px 14px;
border-radius: 8px;
font-family: monospace;
font-size: 0.95rem;
word-break: break-all;
margin-bottom: 10px;
color: #667eea;
font-weight: 600;
}
.btn-copy, .btn-refresh {
background: #667eea;
color: #fff;
border: none;
padding: 8px 18px;
border-radius: 8px;
cursor: pointer;
font-size: 0.9rem;
transition: background 0.2s;
}
.btn-copy:hover, .btn-refresh:hover {
background: #5568d3;
}
/* 上传区域 */
.drop-zone {
border: 2px dashed #c0c4cc;
border-radius: 12px;
padding: 40px 20px;
text-align: center;
cursor: pointer;
transition: all 0.2s;
color: #666;
}
.drop-zone:hover, .drop-zone.dragover {
border-color: #667eea;
background: #f8f9ff;
}
.drop-icon {
font-size: 3rem;
margin-bottom: 12px;
}
.link {
color: #667eea;
font-weight: 600;
text-decoration: underline;
}
.hint {
margin-top: 8px;
font-size: 0.85rem;
color: #999;
}
/* 进度条 */
.progress-container {
margin-top: 16px;
}
.progress-info {
display: flex;
justify-content: space-between;
font-size: 0.9rem;
color: #555;
margin-bottom: 6px;
}
.progress-bar {
height: 8px;
background: #eef0f5;
border-radius: 4px;
overflow: hidden;
}
.progress-fill {
height: 100%;
width: 0;
background: linear-gradient(90deg, #667eea, #764ba2);
transition: width 0.15s;
}
/* 文件列表 */
.files-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 16px;
}
.files-header h2 {
font-size: 1.15rem;
color: #333;
}
.file-list {
list-style: none;
}
.file-list li {
display: flex;
align-items: center;
justify-content: space-between;
padding: 12px 14px;
border-radius: 10px;
background: #f8f9fb;
margin-bottom: 8px;
transition: background 0.2s;
}
.file-list li:hover {
background: #eef0f5;
}
.file-list li.empty {
justify-content: center;
color: #999;
background: transparent;
}
.file-info {
display: flex;
align-items: center;
gap: 12px;
flex: 1;
min-width: 0;
}
.file-icon {
font-size: 1.4rem;
flex-shrink: 0;
}
.file-meta {
min-width: 0;
flex: 1;
}
.file-name {
font-size: 0.95rem;
color: #333;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
font-weight: 500;
}
.file-size {
font-size: 0.8rem;
color: #999;
margin-top: 2px;
}
.file-actions {
display: flex;
gap: 8px;
flex-shrink: 0;
}
.btn-download, .btn-delete {
border: none;
padding: 6px 12px;
border-radius: 6px;
font-size: 0.85rem;
cursor: pointer;
text-decoration: none;
transition: all 0.2s;
}
.btn-download {
background: #667eea;
color: #fff;
}
.btn-download:hover {
background: #5568d3;
}
.btn-delete {
background: #fee;
color: #e74c3c;
}
.btn-delete:hover {
background: #fdd;
}
/* 移动端 */
@media (max-width: 600px) {
header h1 {
font-size: 1.6rem;
}
section {
padding: 18px;
}
.qr-section {
flex-direction: column;
}
.file-actions {
flex-direction: column;
}
}