72 lines
2.9 KiB
HTML
72 lines
2.9 KiB
HTML
<!DOCTYPE html>
|
|||
|
|
<html>
|
||
|
|
<head>
|
||
|
|
<title>文件上传测试</title>
|
||
|
|
<style>
|
||
|
|
body { font-family: Arial; margin: 40px; }
|
||
|
|
.container { max-width: 600px; margin: 0 auto; }
|
||
|
|
.form-group { margin-bottom: 20px; }
|
||
|
|
input[type="file"] { margin: 10px 0; }
|
||
|
|
button { padding: 10px 20px; background: #1890ff; color: white; border: none; border-radius: 4px; cursor: pointer; }
|
||
|
|
.result { margin-top: 20px; padding: 10px; border: 1px solid #ddd; border-radius: 4px; }
|
||
|
|
.success { background: #f6ffed; border-color: #b7eb8f; color: #52c41a; }
|
||
|
|
.error { background: #fff2f0; border-color: #ffccc7; color: #ff4d4f; }
|
||
|
|
</style>
|
||
|
|
</head>
|
||
|
|
<body>
|
||
|
|
<div class="container">
|
||
|
|
<h1>文件上传测试</h1>
|
||
|
|
<form id="uploadForm">
|
||
|
|
<div class="form-group">
|
||
|
|
<label>选择文件:</label>
|
||
|
|
<input type="file" id="fileInput" name="file">
|
||
|
|
</div>
|
||
|
|
<button type="submit">上传文件</button>
|
||
|
|
</form>
|
||
|
|
<div id="result" class="result"></div>
|
||
|
|
</div>
|
||
|
|
<script>
|
||
|
|
document.getElementById('uploadForm').addEventListener('submit', async (e) => {
|
||
|
|
e.preventDefault();
|
||
|
|
const fileInput = document.getElementById('fileInput');
|
||
|
|
const resultDiv = document.getElementById('result');
|
||
|
|
|
||
|
|
if (!fileInput.files.length) {
|
||
|
|
resultDiv.textContent = '请选择一个文件';
|
||
|
|
resultDiv.className = 'result error';
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
const formData = new FormData();
|
||
|
|
formData.append('file', fileInput.files[0]);
|
||
|
|
|
||
|
|
try {
|
||
|
|
const response = await fetch('/api/upload/single', {
|
||
|
|
method: 'POST',
|
||
|
|
body: formData
|
||
|
|
});
|
||
|
|
|
||
|
|
const data = await response.json();
|
||
|
|
|
||
|
|
if (data.success) {
|
||
|
|
resultDiv.innerHTML = `
|
||
|
|
<div class="success">
|
||
|
|
<h3>上传成功!</h3>
|
||
|
|
<p>文件URL: <a href="${data.data.url}" target="_blank">${data.data.url}</a></p>
|
||
|
|
<p>文件名: ${data.data.name}</p>
|
||
|
|
<p>文件大小: ${data.data.size} bytes</p>
|
||
|
|
${data.data.isImage ? `<img src="${data.data.url}" style="max-width: 200px; margin-top: 10px;">` : ''}
|
||
|
|
</div>
|
||
|
|
`;
|
||
|
|
} else {
|
||
|
|
resultDiv.textContent = `上传失败: ${data.error}`;
|
||
|
|
resultDiv.className = 'result error';
|
||
|
|
}
|
||
|
|
} catch (error) {
|
||
|
|
resultDiv.textContent = `上传失败: ${error.message}`;
|
||
|
|
resultDiv.className = 'result error';
|
||
|
|
}
|
||
|
|
});
|
||
|
|
</script>
|
||
|
|
</body>
|
||
|
|
</html>
|