mirror of
https://github.com/Kakune55/Pixel.git
synced 2025-05-06 18:29:25 +08:00
完成基本上传功能
This commit is contained in:
parent
dc2c4ec282
commit
46a546c631
201
Web/upload.gtpl
Normal file
201
Web/upload.gtpl
Normal file
@ -0,0 +1,201 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>文件上传</title>
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
|
||||
<style>
|
||||
body {
|
||||
font-family: 'Arial', sans-serif;
|
||||
background-color: #f5f5f5;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
height: 100vh;
|
||||
}
|
||||
|
||||
header {
|
||||
background-color: #333;
|
||||
color: #fff;
|
||||
padding: 15px;
|
||||
text-align: center;
|
||||
width: 100%;
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
}
|
||||
|
||||
#upload-container {
|
||||
width: 100%;
|
||||
max-width: 400px;
|
||||
background-color: #fff;
|
||||
padding: 20px;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
||||
margin-top: 80px; /* 顶栏高度 + 20px 的间距 */
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
#upload-form {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
#file-input {
|
||||
display: none;
|
||||
}
|
||||
|
||||
#custom-file-input {
|
||||
background-color: #4caf50;
|
||||
color: #fff;
|
||||
border: none;
|
||||
padding: 10px;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
transition: background-color 0.3s;
|
||||
display: inline-block;
|
||||
font-size: 16px;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
#custom-file-input:hover {
|
||||
background-color: #45a049;
|
||||
}
|
||||
|
||||
#upload-button {
|
||||
background-color: #4caf50;
|
||||
color: #fff;
|
||||
border: none;
|
||||
padding: 10px;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
transition: background-color 0.3s;
|
||||
font-size: 16px;
|
||||
}
|
||||
|
||||
#upload-button:hover {
|
||||
background-color: #45a049;
|
||||
}
|
||||
|
||||
#progress-container {
|
||||
width: 100%;
|
||||
margin-top: 20px;
|
||||
display: none;
|
||||
}
|
||||
|
||||
#progress-bar {
|
||||
width: 0;
|
||||
height: 20px;
|
||||
background-color: #4caf50;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
#thumbnail-container {
|
||||
margin-top: 20px;
|
||||
max-width: 100%;
|
||||
height: auto;
|
||||
display: none; /* 默认隐藏 */
|
||||
}
|
||||
|
||||
#thumbnail {
|
||||
max-width: 100%;
|
||||
height: auto;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<header>
|
||||
<h2>Pixel</h2>
|
||||
<!-- 添加帮助和用户信息的按钮/链接等 -->
|
||||
</header>
|
||||
|
||||
<div id="upload-container">
|
||||
<form id="upload-form">
|
||||
<label id="custom-file-input" for="file-input"><i class="fas fa-upload"></i>选择文件</label>
|
||||
<input type="file" id="file-input" accept="image/*" onchange="displayThumbnail()">
|
||||
<button id="upload-button" type="button" onclick="uploadFile()">上传文件</button>
|
||||
</form>
|
||||
|
||||
<div id="progress-container">
|
||||
<div id="progress-bar"></div>
|
||||
</div>
|
||||
|
||||
<div id="thumbnail-container">
|
||||
<img id="thumbnail" alt="缩略图">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
function uploadFile() {
|
||||
const fileInput = document.getElementById('file-input');
|
||||
const progressBar = document.getElementById('progress-bar');
|
||||
const progressContainer = document.getElementById('progress-container');
|
||||
|
||||
const file = fileInput.files[0];
|
||||
if (!file) {
|
||||
alert('请选择文件');
|
||||
return;
|
||||
}
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
|
||||
const xhr = new XMLHttpRequest();
|
||||
|
||||
xhr.upload.onprogress = function (event) {
|
||||
if (event.lengthComputable) {
|
||||
const percentComplete = (event.loaded / event.total) * 100;
|
||||
progressBar.style.width = percentComplete + '%';
|
||||
}
|
||||
};
|
||||
|
||||
xhr.onreadystatechange = function () {
|
||||
if (xhr.readyState === XMLHttpRequest.DONE) {
|
||||
if (xhr.status === 200) {
|
||||
alert('文件上传成功!');
|
||||
} else {
|
||||
alert('文件上传失败!');
|
||||
}
|
||||
|
||||
// 重置进度条
|
||||
progressBar.style.width = '0%';
|
||||
// 隐藏进度条
|
||||
progressContainer.style.display = 'none';
|
||||
}
|
||||
};
|
||||
|
||||
// 显示进度条
|
||||
progressContainer.style.display = 'block';
|
||||
|
||||
// 模拟后端接口,你需要将以下URL替换为实际的后端上传接口
|
||||
xhr.open('POST', '/upload', true);
|
||||
xhr.send(formData);
|
||||
}
|
||||
|
||||
function displayThumbnail() {
|
||||
const fileInput = document.getElementById('file-input');
|
||||
const thumbnail = document.getElementById('thumbnail');
|
||||
const thumbnailContainer = document.getElementById('thumbnail-container');
|
||||
|
||||
const file = fileInput.files[0];
|
||||
if (file && file.type.startsWith('image/')) {
|
||||
const reader = new FileReader();
|
||||
reader.onload = function (e) {
|
||||
thumbnail.src = e.target.result;
|
||||
thumbnailContainer.style.display = 'block';
|
||||
};
|
||||
reader.readAsDataURL(file);
|
||||
} else {
|
||||
thumbnailContainer.style.display = 'none';
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
5
go.mod
Normal file
5
go.mod
Normal file
@ -0,0 +1,5 @@
|
||||
module Pixel
|
||||
|
||||
go 1.21.3
|
||||
|
||||
require github.com/mattn/go-sqlite3 v1.14.19 // indirect
|
2
go.sum
Normal file
2
go.sum
Normal file
@ -0,0 +1,2 @@
|
||||
github.com/mattn/go-sqlite3 v1.14.19 h1:fhGleo2h1p8tVChob4I9HpmVFIAkKGpiukdrgQbWfGI=
|
||||
github.com/mattn/go-sqlite3 v1.14.19/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
54
main.go
Normal file
54
main.go
Normal file
@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
||||
func main() {
|
||||
|
||||
http.HandleFunc("/upload", upload) //设置访问的路由
|
||||
err := http.ListenAndServe(":9090", nil) //设置监听的端口
|
||||
if err != nil {
|
||||
log.Fatal("ListenAndServe: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 处理/upload 逻辑
|
||||
func upload(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println("method:", r.Method) //获取请求的方法
|
||||
if r.Method == "GET" { //前端页面渲染
|
||||
crutime := time.Now().Unix()
|
||||
h := md5.New()
|
||||
io.WriteString(h, strconv.FormatInt(crutime, 10))
|
||||
token := fmt.Sprintf("%x", h.Sum(nil))
|
||||
|
||||
t, _ := template.ParseFiles("Web/upload.gtpl")
|
||||
t.Execute(w, token)
|
||||
} else { //后端POST接收逻辑
|
||||
r.ParseMultipartForm(32 << 20)
|
||||
file, handler, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
fmt.Fprintf(w, "%v", handler.Header)
|
||||
f, err := os.OpenFile("./test/"+handler.Filename, os.O_WRONLY|os.O_CREATE, 0666) // 此处假设当前目录下已存在test目录
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
io.Copy(f, file)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user