341 lines
11 KiB
HTML
341 lines
11 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>TopFans - OSS 文件上传测试</title>
|
|
<style>
|
|
* {
|
|
margin: 0;
|
|
padding: 0;
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
body {
|
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
min-height: 100vh;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
padding: 20px;
|
|
}
|
|
|
|
.container {
|
|
background: white;
|
|
border-radius: 16px;
|
|
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
|
|
max-width: 600px;
|
|
width: 100%;
|
|
padding: 40px;
|
|
}
|
|
|
|
h1 {
|
|
color: #333;
|
|
margin-bottom: 10px;
|
|
font-size: 28px;
|
|
}
|
|
|
|
.subtitle {
|
|
color: #666;
|
|
margin-bottom: 30px;
|
|
font-size: 14px;
|
|
}
|
|
|
|
.form-group {
|
|
margin-bottom: 25px;
|
|
}
|
|
|
|
label {
|
|
display: block;
|
|
margin-bottom: 8px;
|
|
font-weight: 600;
|
|
color: #444;
|
|
font-size: 14px;
|
|
}
|
|
|
|
input[type="text"],
|
|
input[type="file"],
|
|
select {
|
|
width: 100%;
|
|
padding: 12px 16px;
|
|
border: 2px solid #e1e8ed;
|
|
border-radius: 8px;
|
|
font-size: 14px;
|
|
transition: all 0.3s ease;
|
|
font-family: inherit;
|
|
}
|
|
|
|
input[type="text"]:focus,
|
|
select:focus {
|
|
outline: none;
|
|
border-color: #667eea;
|
|
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
|
|
}
|
|
|
|
input[type="file"] {
|
|
padding: 10px;
|
|
cursor: pointer;
|
|
}
|
|
|
|
button {
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
color: white;
|
|
padding: 14px 28px;
|
|
border: none;
|
|
border-radius: 8px;
|
|
cursor: pointer;
|
|
font-size: 16px;
|
|
font-weight: 600;
|
|
width: 100%;
|
|
transition: transform 0.2s ease, box-shadow 0.2s ease;
|
|
}
|
|
|
|
button:hover {
|
|
transform: translateY(-2px);
|
|
box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
|
|
}
|
|
|
|
button:active {
|
|
transform: translateY(0);
|
|
}
|
|
|
|
button:disabled {
|
|
opacity: 0.6;
|
|
cursor: not-allowed;
|
|
transform: none;
|
|
}
|
|
|
|
.status {
|
|
margin-top: 25px;
|
|
padding: 16px;
|
|
border-radius: 8px;
|
|
font-size: 14px;
|
|
line-height: 1.6;
|
|
display: none;
|
|
animation: slideIn 0.3s ease;
|
|
}
|
|
|
|
@keyframes slideIn {
|
|
from {
|
|
opacity: 0;
|
|
transform: translateY(-10px);
|
|
}
|
|
to {
|
|
opacity: 1;
|
|
transform: translateY(0);
|
|
}
|
|
}
|
|
|
|
.status.show {
|
|
display: block;
|
|
}
|
|
|
|
.status.info {
|
|
background-color: #e3f2fd;
|
|
color: #1565c0;
|
|
border-left: 4px solid #1565c0;
|
|
}
|
|
|
|
.status.success {
|
|
background-color: #e8f5e9;
|
|
color: #2e7d32;
|
|
border-left: 4px solid #2e7d32;
|
|
}
|
|
|
|
.status.error {
|
|
background-color: #ffebee;
|
|
color: #c62828;
|
|
border-left: 4px solid #c62828;
|
|
}
|
|
|
|
.status a {
|
|
color: inherit;
|
|
word-break: break-all;
|
|
text-decoration: underline;
|
|
}
|
|
|
|
.help-text {
|
|
font-size: 12px;
|
|
color: #888;
|
|
margin-top: 5px;
|
|
}
|
|
|
|
.loading {
|
|
display: inline-block;
|
|
width: 20px;
|
|
height: 20px;
|
|
border: 3px solid rgba(255, 255, 255, 0.3);
|
|
border-radius: 50%;
|
|
border-top-color: white;
|
|
animation: spin 1s linear infinite;
|
|
margin-right: 8px;
|
|
vertical-align: middle;
|
|
}
|
|
|
|
@keyframes spin {
|
|
to { transform: rotate(360deg); }
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<h1>🚀 TopFans OSS 上传测试</h1>
|
|
<p class="subtitle">测试头像和资产文件上传到阿里云 OSS</p>
|
|
|
|
<form id="uploadForm">
|
|
<div class="form-group">
|
|
<label for="jwtToken">JWT Token <span style="color: #c62828;">*</span></label>
|
|
<input type="text" id="jwtToken" name="jwtToken"
|
|
placeholder="请输入您的 JWT Token" required />
|
|
<div class="help-text">从登录接口获取的 JWT Token</div>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="uploadType">上传类型 <span style="color: #c62828;">*</span></label>
|
|
<select id="uploadType" name="uploadType">
|
|
<option value="avatar">👤 头像 (avatar)</option>
|
|
<option value="asset">🎨 资产 (asset)</option>
|
|
</select>
|
|
<div class="help-text">选择文件上传的目标目录</div>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="file">选择文件 <span style="color: #c62828;">*</span></label>
|
|
<input type="file" id="file" name="file" required />
|
|
<div class="help-text">支持的文件类型:图片、视频等</div>
|
|
</div>
|
|
|
|
<button type="submit" id="submitBtn">
|
|
<span id="btnText">上传文件</span>
|
|
</button>
|
|
</form>
|
|
|
|
<div id="statusDiv" class="status"></div>
|
|
</div>
|
|
|
|
<script type="text/javascript">
|
|
document.addEventListener('DOMContentLoaded', function () {
|
|
const form = document.querySelector("#uploadForm");
|
|
const fileInput = document.querySelector("#file");
|
|
const jwtTokenInput = document.querySelector("#jwtToken");
|
|
const uploadTypeInput = document.querySelector("#uploadType");
|
|
const statusDiv = document.querySelector("#statusDiv");
|
|
const submitBtn = document.querySelector("#submitBtn");
|
|
const btnText = document.querySelector("#btnText");
|
|
|
|
form.addEventListener("submit", async (event) => {
|
|
event.preventDefault();
|
|
|
|
const file = fileInput.files[0];
|
|
const jwtToken = jwtTokenInput.value.trim();
|
|
const uploadType = uploadTypeInput.value;
|
|
|
|
if (!file) {
|
|
showStatus('❌ 请选择一个文件再上传。', 'error');
|
|
return;
|
|
}
|
|
|
|
if (!jwtToken) {
|
|
showStatus('❌ 请输入 JWT Token。', 'error');
|
|
return;
|
|
}
|
|
|
|
// 禁用提交按钮
|
|
submitBtn.disabled = true;
|
|
btnText.innerHTML = '<span class="loading"></span>上传中...';
|
|
|
|
try {
|
|
showStatus('⏳ 正在获取上传签名...', 'info');
|
|
|
|
// 1. 获取签名
|
|
const signResponse = await fetch(
|
|
`http://localhost:8080/api/v1/assets/oss/signature?type=${uploadType}`,
|
|
{
|
|
method: "GET",
|
|
headers: {
|
|
'Authorization': `Bearer ${jwtToken}`
|
|
}
|
|
}
|
|
);
|
|
|
|
if (!signResponse.ok) {
|
|
throw new Error(`获取签名失败: HTTP ${signResponse.status}`);
|
|
}
|
|
|
|
const signData = await signResponse.json();
|
|
|
|
if (signData.code !== 200) {
|
|
throw new Error(signData.message || '获取签名失败');
|
|
}
|
|
|
|
console.log('✅ 签名获取成功:', signData);
|
|
|
|
// 2. 构建表单数据
|
|
showStatus('📤 正在上传文件到 OSS...', 'info');
|
|
|
|
const formData = new FormData();
|
|
// 注意:字段顺序很重要!
|
|
formData.append("key", signData.data.dir + file.name); // 文件路径
|
|
formData.append("policy", signData.data.policy);
|
|
formData.append("success_action_status", "200");
|
|
formData.append("x-oss-credential", signData.data.x_oss_credential);
|
|
formData.append("x-oss-date", signData.data.x_oss_date);
|
|
formData.append("x-oss-security-token", signData.data.security_token);
|
|
formData.append("x-oss-signature", signData.data.signature); // 正确的字段名
|
|
formData.append("x-oss-signature-version", signData.data.x_oss_signature_version);
|
|
formData.append("file", file); // file 必须为最后一个表单域
|
|
|
|
console.log('📦 上传信息:', {
|
|
host: signData.data.host,
|
|
dir: signData.data.dir,
|
|
filename: file.name,
|
|
fullPath: signData.data.dir + file.name
|
|
});
|
|
|
|
// 3. 上传到 OSS
|
|
const uploadResponse = await fetch(signData.data.host, {
|
|
method: "POST",
|
|
body: formData
|
|
});
|
|
|
|
if (uploadResponse.ok || uploadResponse.status === 204) {
|
|
const fileUrl = `${signData.data.host}/${signData.data.dir}${file.name}`;
|
|
showStatus(
|
|
`✅ <strong>文件上传成功!</strong><br><br>
|
|
<strong>文件类型:</strong> ${uploadType}<br>
|
|
<strong>文件名:</strong> ${file.name}<br>
|
|
<strong>文件大小:</strong> ${(file.size / 1024).toFixed(2)} KB<br><br>
|
|
<strong>文件 URL:</strong><br>
|
|
<a href="${fileUrl}" target="_blank">${fileUrl}</a>`,
|
|
'success'
|
|
);
|
|
console.log('✅ 上传成功:', fileUrl);
|
|
|
|
// 重置表单
|
|
fileInput.value = '';
|
|
} else {
|
|
const errorText = await uploadResponse.text();
|
|
console.error("❌ 上传失败", uploadResponse, errorText);
|
|
showStatus(`❌ <strong>上传失败</strong><br>${errorText || '未知错误'}`, 'error');
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error("❌ 发生错误:", error);
|
|
showStatus(`❌ <strong>发生错误:</strong><br>${error.message}`, 'error');
|
|
} finally {
|
|
// 恢复提交按钮
|
|
submitBtn.disabled = false;
|
|
btnText.textContent = '上传文件';
|
|
}
|
|
});
|
|
|
|
function showStatus(message, type) {
|
|
statusDiv.innerHTML = message;
|
|
statusDiv.className = `status ${type} show`;
|
|
}
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|