<!DOCTYPE html>
|
<html lang="zh-CN">
|
<head>
|
<meta charset="UTF-8">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<title>文件上传测试</title>
|
<style>
|
body {
|
font-family: Arial, sans-serif;
|
max-width: 800px;
|
margin: 0 auto;
|
padding: 20px;
|
}
|
.upload-form {
|
border: 2px dashed #ccc;
|
padding: 20px;
|
text-align: center;
|
margin-bottom: 20px;
|
}
|
.upload-form:hover {
|
border-color: #999;
|
}
|
.form-group {
|
margin-bottom: 15px;
|
}
|
label {
|
display: block;
|
margin-bottom: 5px;
|
font-weight: bold;
|
}
|
input[type="text"], input[type="file"] {
|
width: 100%;
|
padding: 8px;
|
border: 1px solid #ddd;
|
border-radius: 4px;
|
box-sizing: border-box;
|
}
|
button {
|
background-color: #4CAF50;
|
color: white;
|
padding: 10px 20px;
|
border: none;
|
border-radius: 4px;
|
cursor: pointer;
|
font-size: 16px;
|
}
|
button:hover {
|
background-color: #45a049;
|
}
|
button:disabled {
|
background-color: #cccccc;
|
cursor: not-allowed;
|
}
|
.result {
|
margin-top: 20px;
|
padding: 15px;
|
border-radius: 4px;
|
}
|
.success {
|
background-color: #d4edda;
|
border: 1px solid #c3e6cb;
|
color: #155724;
|
}
|
.error {
|
background-color: #f8d7da;
|
border: 1px solid #f5c6cb;
|
color: #721c24;
|
}
|
.progress {
|
width: 100%;
|
height: 20px;
|
background-color: #f0f0f0;
|
border-radius: 10px;
|
overflow: hidden;
|
margin-top: 10px;
|
}
|
.progress-bar {
|
height: 100%;
|
background-color: #4CAF50;
|
width: 0%;
|
transition: width 0.3s ease;
|
}
|
.file-info {
|
margin-top: 10px;
|
font-size: 14px;
|
color: #666;
|
}
|
</style>
|
</head>
|
<body>
|
<h1>文件上传测试</h1>
|
|
<div class="upload-form">
|
<form id="uploadForm" enctype="multipart/form-data">
|
<div class="form-group">
|
<label for="uploadFileName">文件名前缀:</label>
|
<input type="text" id="uploadFileName" name="uploadFileName" placeholder="例如:test123" required>
|
</div>
|
|
<div class="form-group">
|
<label for="file">选择文件:</label>
|
<input type="file" id="file" name="file" required>
|
<div class="file-info">
|
支持的文件类型:JPG, PNG, GIF, PDF, DOC, DOCX<br>
|
最大文件大小:10MB
|
</div>
|
</div>
|
|
<button type="submit" id="submitBtn">上传文件</button>
|
</form>
|
|
<div class="progress" id="progress" style="display: none;">
|
<div class="progress-bar" id="progressBar"></div>
|
</div>
|
</div>
|
|
<div id="result"></div>
|
|
<script>
|
document.getElementById('uploadForm').addEventListener('submit', function(e) {
|
e.preventDefault();
|
|
const formData = new FormData();
|
const uploadFileName = document.getElementById('uploadFileName').value;
|
const file = document.getElementById('file').files[0];
|
|
if (!uploadFileName || !file) {
|
showResult('请填写文件名前缀并选择文件', false);
|
return;
|
}
|
|
formData.append('uploadFileName', uploadFileName);
|
formData.append('file', file);
|
|
// 显示进度条
|
document.getElementById('progress').style.display = 'block';
|
document.getElementById('submitBtn').disabled = true;
|
|
// 模拟进度
|
let progress = 0;
|
const progressInterval = setInterval(() => {
|
progress += Math.random() * 10;
|
if (progress > 90) progress = 90;
|
document.getElementById('progressBar').style.width = progress + '%';
|
}, 100);
|
|
fetch('upload_file.php', {
|
method: 'POST',
|
body: formData
|
})
|
.then(response => response.json())
|
.then(data => {
|
clearInterval(progressInterval);
|
document.getElementById('progressBar').style.width = '100%';
|
|
setTimeout(() => {
|
document.getElementById('progress').style.display = 'none';
|
document.getElementById('submitBtn').disabled = false;
|
|
if (data.success) {
|
showResult(`
|
<h3>上传成功!</h3>
|
<p><strong>原始文件名:</strong>${data.data.originalName}</p>
|
<p><strong>保存文件名:</strong>${data.data.fileName}</p>
|
<p><strong>文件大小:</strong>${formatFileSize(data.data.fileSize)}</p>
|
<p><strong>文件类型:</strong>${data.data.fileType}</p>
|
<p><strong>上传时间:</strong>${data.data.uploadTime}</p>
|
<p><strong>文件路径:</strong><a href="${data.data.filePath}" target="_blank">${data.data.filePath}</a></p>
|
${data.data.thumbnailPath ? `<p><strong>缩略图:</strong><a href="${data.data.thumbnailPath}" target="_blank">${data.data.thumbnailPath}</a></p>` : ''}
|
`, true);
|
} else {
|
showResult('上传失败:' + data.message, false);
|
}
|
}, 500);
|
})
|
.catch(error => {
|
clearInterval(progressInterval);
|
document.getElementById('progress').style.display = 'none';
|
document.getElementById('submitBtn').disabled = false;
|
showResult('网络错误:' + error.message, false);
|
});
|
});
|
|
function showResult(message, isSuccess) {
|
const resultDiv = document.getElementById('result');
|
resultDiv.innerHTML = message;
|
resultDiv.className = 'result ' + (isSuccess ? 'success' : 'error');
|
}
|
|
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];
|
}
|
|
// 文件选择时显示文件信息
|
document.getElementById('file').addEventListener('change', function(e) {
|
const file = e.target.files[0];
|
if (file) {
|
const fileInfo = document.querySelector('.file-info');
|
fileInfo.innerHTML = `
|
已选择文件:${file.name}<br>
|
文件大小:${formatFileSize(file.size)}<br>
|
文件类型:${file.type}<br>
|
支持的文件类型:JPG, PNG, GIF, PDF, DOC, DOCX<br>
|
最大文件大小:10MB
|
`;
|
}
|
});
|
</script>
|
</body>
|
</html>
|