159 lines
4.8 KiB
HTML
159 lines
4.8 KiB
HTML
<!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: 600px;
|
|
margin: 0 auto;
|
|
padding: 20px;
|
|
line-height: 1.6;
|
|
}
|
|
.upload-container {
|
|
border: 2px dashed #ccc;
|
|
padding: 30px;
|
|
text-align: center;
|
|
border-radius: 5px;
|
|
margin-bottom: 20px;
|
|
}
|
|
.upload-btn {
|
|
background-color: #4CAF50;
|
|
color: white;
|
|
padding: 10px 20px;
|
|
border: none;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
font-size: 16px;
|
|
}
|
|
.upload-btn:hover {
|
|
background-color: #45a049;
|
|
}
|
|
#file-info {
|
|
margin-top: 10px;
|
|
font-size: 14px;
|
|
color: #666;
|
|
}
|
|
.progress-container {
|
|
margin-top: 20px;
|
|
display: none;
|
|
}
|
|
.progress-bar {
|
|
width: 100%;
|
|
background-color: #f1f1f1;
|
|
border-radius: 4px;
|
|
margin-bottom: 10px;
|
|
}
|
|
#progress {
|
|
height: 20px;
|
|
background-color: #4CAF50;
|
|
border-radius: 4px;
|
|
width: 0%;
|
|
text-align: center;
|
|
line-height: 20px;
|
|
color: white;
|
|
}
|
|
#status {
|
|
font-size: 14px;
|
|
color: #666;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<h1>文件上传</h1>
|
|
|
|
<form id="uploadForm" enctype="multipart/form-data">
|
|
<div class="upload-container">
|
|
<input type="file" id="fileInput" name="file" style="display: none;">
|
|
<label for="fileInput" class="upload-btn">选择文件</label>
|
|
<div id="file-info">未选择文件</div>
|
|
</div>
|
|
|
|
<div class="progress-container" id="progressContainer">
|
|
<div class="progress-bar">
|
|
<div id="progress">0%</div>
|
|
</div>
|
|
<div id="status">准备上传...</div>
|
|
</div>
|
|
|
|
<button type="submit" class="upload-btn">上传文件</button>
|
|
</form>
|
|
|
|
<script>
|
|
document.getElementById('fileInput').addEventListener('change', function(e) {
|
|
const fileInfo = document.getElementById('file-info');
|
|
if (this.files.length > 0) {
|
|
const file = this.files[0];
|
|
fileInfo.textContent = `已选择: ${file.name} (${formatFileSize(file.size)})`;
|
|
} else {
|
|
fileInfo.textContent = '未选择文件';
|
|
}
|
|
});
|
|
|
|
document.getElementById('uploadForm').addEventListener('submit', function(e) {
|
|
e.preventDefault();
|
|
|
|
const fileInput = document.getElementById('fileInput');
|
|
if (fileInput.files.length === 0) {
|
|
alert('请先选择文件');
|
|
return;
|
|
}
|
|
|
|
const file = fileInput.files[0];
|
|
const formData = new FormData();
|
|
formData.append('file', file);
|
|
|
|
const progressContainer = document.getElementById('progressContainer');
|
|
const progressBar = document.getElementById('progress');
|
|
const status = document.getElementById('status');
|
|
|
|
progressContainer.style.display = 'block';
|
|
status.textContent = '上传中...';
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
xhr.open('POST', 'http://chat.qxmier.com/adminapi/UploadFile/file_upload', true);
|
|
|
|
xhr.upload.onprogress = function(e) {
|
|
if (e.lengthComputable) {
|
|
const percent = Math.round((e.loaded / e.total) * 100);
|
|
progressBar.style.width = percent + '%';
|
|
progressBar.textContent = percent + '%';
|
|
}
|
|
};
|
|
|
|
xhr.onload = function() {
|
|
if (xhr.status === 200) {
|
|
status.textContent = '上传成功!';
|
|
try {
|
|
const response = JSON.parse(xhr.responseText);
|
|
console.log('服务器响应:', response);
|
|
alert('文件上传成功!');
|
|
} catch (e) {
|
|
console.log('响应解析失败:', e);
|
|
}
|
|
} else {
|
|
status.textContent = '上传失败: ' + xhr.statusText;
|
|
alert('上传失败: ' + xhr.statusText);
|
|
}
|
|
};
|
|
|
|
xhr.onerror = function() {
|
|
status.textContent = '上传过程中发生错误';
|
|
alert('上传过程中发生错误');
|
|
};
|
|
|
|
xhr.send(formData);
|
|
});
|
|
|
|
function formatFileSize(bytes) {
|
|
if (bytes === 0) return '0 Bytes';
|
|
const k = 1024;
|
|
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
|
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
|
}
|
|
</script>
|
|
</body>
|
|
</html> |