WorkerJS_CloudFlare_ImageBed/cloudflare-page/OneAPI-imgbed-MIX.html

136 lines
5.5 KiB
HTML
Raw Normal View History

2024-03-26 14:11:18 +08:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文件上传</title>
<!-- 引入 Bootstrap CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.min.css">
<style>
.container {
margin-top: 50px;
}
.result-item {
2024-03-26 14:20:02 +08:00
margin-top: 10px;
2024-03-26 14:31:26 +08:00
border: 1px solid #ccc;
padding: 10px;
border-radius: 5px;
2024-03-26 14:35:42 +08:00
display: flex;
align-items: center;
2024-03-26 14:45:43 +08:00
flex-wrap: wrap; /* 允许多行显示 */
2024-03-26 14:35:42 +08:00
}
2024-03-26 14:43:21 +08:00
.preview-container {
2024-03-26 14:45:43 +08:00
max-width: calc(100% - 120px); /* 图片预览容器的最大宽度为外部框架宽度减去按钮和文件名的宽度 */
2024-03-26 14:40:12 +08:00
max-height: calc(8 * 50px); /* 最大高度不超过8个上传按钮的高度 */
2024-03-26 14:43:21 +08:00
overflow: hidden; /* 超出部分隐藏 */
2024-03-26 14:45:43 +08:00
margin-top: 10px;
2024-03-26 14:43:21 +08:00
}
.preview-img {
max-width: 100%; /* 图片预览的最大宽度为容器宽度 */
height: auto; /* 自适应高度 */
display: block; /* 防止图片下面产生空白 */
2024-03-26 14:11:18 +08:00
}
2024-03-26 14:40:12 +08:00
.copy-button {
2024-03-26 14:45:43 +08:00
width: 100px; /* 设置复制按钮的宽度 */
margin-right: 10px; /* 添加一些右侧边距 */
2024-03-26 14:40:12 +08:00
}
2024-03-26 14:11:18 +08:00
</style>
</head>
<body>
<div class="container">
<h2>文件上传</h2>
<form id="upload-form">
<div class="form-group">
2024-03-26 14:20:02 +08:00
<label for="apiUrl">API 域名:</label>
2024-03-26 14:11:18 +08:00
<input type="text" class="form-control" id="apiUrl" placeholder="输入 API 域名" required>
</div>
<div class="form-group">
<label for="fileInput">选择文件</label>
<input type="file" class="form-control-file" id="fileInput" multiple required>
</div>
<div class="form-group">
<label for="apiSelect">选择接口</label>
<select class="form-control" id="apiSelect">
<option value="58img">api-58img</option>
<option value="tgphimg">api-tgph-official</option>
</select>
</div>
2024-03-26 14:20:02 +08:00
<div class="form-check mb-2">
<input class="form-check-input" type="checkbox" id="enablePreview">
<label class="form-check-label" for="enablePreview">
启用预览
</label>
</div>
2024-03-26 14:11:18 +08:00
<button type="submit" class="btn btn-primary">上传</button>
</form>
2024-03-26 14:20:02 +08:00
<div id="result" class="mt-3">
<!-- 上传结果将显示在这里 -->
2024-03-26 14:11:18 +08:00
</div>
</div>
<!-- 引入 jQuery 和 Bootstrap JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script>
document.getElementById('upload-form').addEventListener('submit', function(e) {
2024-03-26 14:43:21 +08:00
e.preventDefault();
2024-03-26 14:11:18 +08:00
2024-03-26 14:43:21 +08:00
const apiUrlInput = document.getElementById('apiUrl');
const fileInput = document.getElementById('fileInput');
const apiSelect = document.getElementById('apiSelect');
const resultContainer = document.getElementById('result');
const enablePreview = document.getElementById('enablePreview').checked;
2024-03-26 14:11:18 +08:00
2024-03-26 14:43:21 +08:00
// 清除旧的结果
resultContainer.innerHTML = '';
resultContainer.style.display = 'none';
2024-03-26 14:11:18 +08:00
2024-03-26 14:43:21 +08:00
for (const file of fileInput.files) {
const formData = new FormData();
formData.append('image', file);
2024-03-26 14:11:18 +08:00
2024-03-26 14:43:21 +08:00
const apiUrl = `${apiUrlInput.value}/upload/${apiSelect.value}`;
console.log('API URL:', apiUrl);
2024-03-26 14:31:26 +08:00
2024-03-26 14:43:21 +08:00
fetch(apiUrl, {
method: 'POST',
body: formData,
})
.then(response => response.text())
.then(data => {
const resultItem = document.createElement('div');
resultItem.className = 'result-item';
resultItem.innerHTML = `
2024-03-26 14:45:43 +08:00
<button class="btn btn-success copy-button" onclick="copyUrl(this)">复制URL</button>
<span>${file.name}</span>
2024-03-26 14:43:21 +08:00
<div class="preview-container">
<img src="${data}" class="preview-img">
</div>
<input type="text" class="form-control" value="${data}" readonly>
`;
2024-03-26 14:31:26 +08:00
2024-03-26 14:43:21 +08:00
resultContainer.appendChild(resultItem);
resultContainer.style.display = 'flex';
})
.catch(error => {
console.error('Error:', error);
const resultItem = document.createElement('div');
resultItem.className = 'result-item';
resultItem.innerHTML = `<input type="text" class="form-control" value="文件 ${file.name} 上传失败" readonly>`;
resultContainer.appendChild(resultItem);
resultContainer.style.display = 'flex';
});
}
});
2024-03-26 14:20:02 +08:00
2024-03-26 14:43:21 +08:00
function copyUrl(button) {
const copyText = button.nextElementSibling.nextElementSibling;
copyText.select();
copyText.setSelectionRange(0, 99999);
document.execCommand('copy');
2024-03-26 14:32:44 +08:00
}
</script>
</body>
2024-03-26 14:35:42 +08:00
</html>