<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<title>文件上传</title>
<!-- 引入Font Awesome图标 -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
body {
background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.container {
width: 100%;
max-width: 500px;
margin: 0 auto;
}
.upload-card {
background: rgba(255, 255, 255, 0.95);
border-radius: 20px;
box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
padding: 40px 30px;
text-align: center;
transition: all 0.3s ease;
position: relative;
overflow: hidden;
}
.upload-card::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 5px;
background: linear-gradient(90deg, #6a11cb, #2575fc);
}
.card-header {
margin-bottom: 30px;
}
.card-header h1 {
color: #333;
font-size: 28px;
font-weight: 600;
margin-bottom: 8px;
}
.card-header p {
color: #777;
font-size: 16px;
}
.upload-area {
border: 3px dashed #ddd;
border-radius: 15px;
padding: 40px 20px;
margin-bottom: 30px;
cursor: pointer;
transition: all 0.3s ease;
position: relative;
background: #f9f9ff;
}
.upload-area:hover, .upload-area.dragover {
border-color: #6a11cb;
background: #f0f4ff;
}
.upload-icon {
font-size: 70px;
color: #6a11cb;
margin-bottom: 20px;
transition: transform 0.3s ease;
}
.upload-area:hover .upload-icon {
transform: translateY(-5px);
}
.upload-text h3 {
color: #444;
font-size: 20px;
margin-bottom: 10px;
}
.upload-text p {
color: #888;
font-size: 14px;
margin-bottom: 15px;
}
.browse-btn {
display: inline-block;
background: #6a11cb;
color: white;
padding: 10px 25px;
border-radius: 50px;
font-weight: 500;
cursor: pointer;
transition: all 0.3s ease;
box-shadow: 0 4px 15px rgba(106, 17, 203, 0.3);
}
.browse-btn:hover {
background: #2575fc;
transform: translateY(-3px);
box-shadow: 0 6px 20px rgba(37, 117, 252, 0.4);
}
.file-info {
background: #f0f4ff;
border-radius: 12px;
padding: 15px;
margin: 20px 0;
display: none;
text-align: left;
}
.file-info.active {
display: block;
animation: fadeIn 0.5s ease;
}
.file-info h4 {
color: #444;
margin-bottom: 10px;
font-size: 16px;
}
.file-details {
display: flex;
justify-content: space-between;
align-items: center;
}
.file-name {
color: #555;
font-weight: 500;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
max-width: 70%;
}
.file-size {
color: #888;
font-size: 14px;
}
.progress-container {
margin: 25px 0;
display: none;
}
.progress-container.active {
display: block;
animation: fadeIn 0.5s ease;
}
.progress-header {
display: flex;
justify-content: space-between;
margin-bottom: 8px;
color: #555;
font-size: 14px;
}
.progress-bar {
height: 12px;
background: #e0e0e0;
border-radius: 10px;
overflow: hidden;
}
.progress-fill {
height: 100%;
background: linear-gradient(90deg, #6a11cb, #2575fc);
border-radius: 10px;
width: 0%;
transition: width 0.4s ease;
}
.action-buttons {
display: flex;
justify-content: center;
gap: 15px;
margin-top: 20px;
}
.btn {
padding: 12px 30px;
border-radius: 50px;
font-weight: 500;
cursor: pointer;
transition: all 0.3s ease;
border: none;
font-size: 16px;
min-width: 140px;
}
.btn-back {
background: #f1f1f1;
color: #555;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.08);
}
.btn-back:hover {
background: #e0e0e0;
transform: translateY(-3px);
box-shadow: 0 6px 15px rgba(0, 0, 0, 0.1);
}
.btn-upload {
background: linear-gradient(90deg, #6a11cb, #2575fc);
color: white;
box-shadow: 0 4px 15px rgba(106, 17, 203, 0.4);
}
.btn-upload:hover {
transform: translateY(-3px);
box-shadow: 0 6px 20px rgba(37, 117, 252, 0.5);
}
.btn-upload:disabled {
background: #ccc;
cursor: not-allowed;
transform: none;
box-shadow: none;
}
.success-message {
background: #d4edda;
color: #155724;
padding: 15px;
border-radius: 10px;
margin-top: 20px;
display: none;
animation: fadeIn 0.5s ease;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
@media (max-width: 576px) {
.upload-card {
padding: 30px 20px;
}
.action-buttons {
flex-direction: column;
}
.btn {
width: 100%;
}
}
.hidden {
display: none;
}
</style>
</head>
<body>
<div class="container">
<div class="upload-card">
<div class="card-header">
<h1><i class="fas fa-cloud-upload-alt"></i> 文件上传</h1>
<p>选择文件或拖放到下方区域</p>
</div>
<div class="upload-area" id="uploadArea">
<div class="upload-icon">
<i class="fas fa-file-upload"></i>
</div>
<div class="upload-text">
<h3>拖放文件到此处</h3>
<p>支持各种类型的文件,最大不超过100MB</p>
<div class="browse-btn">浏览文件</div>
</div>
<input type="file" id="fileInput" class="hidden">
</div>
<div class="file-info" id="fileInfo">
<h4>已选文件:</h4>
<div class="file-details">
<div class="file-name" id="fileName">未选择文件</div>
<div class="file-size" id="fileSize">0 KB</div>
</div>
</div>
<div class="progress-container" id="progressContainer">
<div class="progress-header">
<span>上传中...</span>
<span id="progressPercent">0%</span>
</div>
<div class="progress-bar">
<div class="progress-fill" id="progressFill"></div>
</div>
</div>
<div class="success-message" id="successMessage">
<i class="fas fa-check-circle"></i> 文件上传成功!即将返回...
</div>
<div class="action-buttons">
<button class="btn btn-back" onclick="navigateBack()">
<i class="fas fa-arrow-left"></i> 返回
</button>
<button class="btn btn-upload" id="uploadBtn" disabled onclick="uploadFile()">
<i class="fas fa-upload"></i> 上传文件
</button>
</div>
</div>
</div>
<script>
// DOM元素
const uploadArea = document.getElementById('uploadArea');
const fileInput = document.getElementById('fileInput');
const fileInfo = document.getElementById('fileInfo');
const fileName = document.getElementById('fileName');
const fileSize = document.getElementById('fileSize');
const progressContainer = document.getElementById('progressContainer');
const progressFill = document.getElementById('progressFill');
const progressPercent = document.getElementById('progressPercent');
const uploadBtn = document.getElementById('uploadBtn');
const successMessage = document.getElementById('successMessage');
// 文件对象
let selectedFile = null;
// 初始化事件监听
function init() {
// 点击上传区域触发文件选择
uploadArea.addEventListener('click', () => {
fileInput.click();
});
// 文件选择变化
fileInput.addEventListener('change', function() {
if (this.files.length > 0) {
handleFileSelection(this.files[0]);
}
});
// 拖放事件
uploadArea.addEventListener('dragover', (e) => {
e.preventDefault();
uploadArea.classList.add('dragover');
});
uploadArea.addEventListener('dragleave', () => {
uploadArea.classList.remove('dragover');
});
uploadArea.addEventListener('drop', (e) => {
e.preventDefault();
uploadArea.classList.remove('dragover');
if (e.dataTransfer.files.length > 0) {
handleFileSelection(e.dataTransfer.files[0]);
}
});
}
// 处理选择的文件
function handleFileSelection(file) {
if (file.size > 100 * 1024 * 1024) {
alert('文件大小不能超过100MB');
return;
}
selectedFile = file;
// 显示文件信息
fileName.textContent = file.name;
fileSize.textContent = formatFileSize(file.size);
fileInfo.classList.add('active');
// 启用上传按钮
uploadBtn.disabled = false;
// 隐藏进度和成功消息(如果显示)
progressContainer.classList.remove('active');
successMessage.style.display = 'none';
}
// 格式化文件大小
function formatFileSize(bytes) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
// 上传文件
function uploadFile() {
if (!selectedFile) return;
// 显示进度条
progressContainer.classList.add('active');
// 模拟上传进度(实际项目中替换为真实上传逻辑)
let progress = 0;
const interval = setInterval(() => {
progress += Math.random() * 10;
if (progress >= 100) {
progress = 100;
clearInterval(interval);
// 上传完成
setTimeout(() => {
successMessage.style.display = 'block';
uploadBtn.disabled = true;
// 2秒后返回(模拟原代码功能)
setTimeout(() => {
navigateBack();
}, 2000);
}, 300);
}
// 更新进度条
progressFill.style.width = progress + '%';
progressPercent.textContent = Math.round(progress) + '%';
}, 200);
}
// 返回函数(原功能)
function navigateBack() {
console.log("返回操作");
// 原uni.navigateBack功能
// uni.navigateBack({ delta: 1 });
// 模拟返回
alert("执行返回操作");
}
// 初始化
window.onload = init;
</script>
</body>
</html> 需支付 5元 阅读剩余内容