<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Upload File to GoFile</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 50px;
}
.upload-container {
max-width: 400px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 10px;
background-color: #f9f9f9;
}
.upload-container h2 {
text-align: center;
}
.upload-container input[type="file"] {
margin-bottom: 20px;
width: 100%;
}
.upload-container button {
display: block;
width: 100%;
padding: 10px;
background-color: #28a745;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
.upload-container button:hover {
background-color: #218838;
}
.progress-container {
margin: 15px 0;
background-color: #e9ecef;
border-radius: 5px;
height: 20px;
display: none;
}
.progress-bar {
height: 100%;
background-color: #4CAF50;
border-radius: 5px;
width: 0%;
transition: width 0.3s ease;
}
.progress-text {
text-align: center;
margin-top: 5px;
font-size: 14px;
}
.result {
margin-top: 20px;
padding: 10px;
background-color: #e9ecef;
border-radius: 5px;
word-wrap: break-word;
}
</style>
</head>
<body>
<div class="upload-container">
<h2>Upload File to GoFile</h2>
<input type="file" id="fileInput">
<button onclick="uploadFile()">Upload</button>
<div class="progress-container">
<div class="progress-bar" id="progressBar"></div>
</div>
<div class="progress-text" id="progressText"></div>
<div class="result" id="result"></div>
</div>
<script>
function uploadFile() {
const fileInput = document.getElementById('fileInput');
const resultDiv = document.getElementById('result');
const progressBar = document.getElementById('progressBar');
const progressContainer = document.querySelector('.progress-container');
const progressText = document.getElementById('progressText');
if (fileInput.files.length === 0) {
resultDiv.textContent = '请选择要上传的文件';
return;
}
const file = fileInput.files[0];
const formData = new FormData();
formData.append('file', file);
// 重置显示
resultDiv.textContent = '';
progressBar.style.width = '0%';
progressText.textContent = '0%';
progressContainer.style.display = 'block';
const xhr = new XMLHttpRequest();
xhr.upload.addEventListener('progress', (e) => {
if (e.lengthComputable) {
const percent = Math.round((e.loaded / e.total) * 100);
progressBar.style.width = percent + '%';
progressText.textContent = percent + '%';
}
});
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
progressContainer.style.display = 'none';
try {
const data = JSON.parse(xhr.responseText);
if (data.status === 'ok') {
const downloadPage = data.data.downloadPage;
resultDiv.innerHTML = `文件上传成功!<a href="${downloadPage}" target="_blank">下载页面</a>`;
} else {
resultDiv.textContent = '上传失败,请重试';
}
} catch (error) {
resultDiv.textContent = '发生错误,请重试';
console.error(error);
}
}
};
xhr.open('POST', 'https://store1.gofile.io/contents/uploadfile', true);
xhr.send(formData);
}
</script>
</body>
</html>
© 版权声明
THE END
暂无评论内容