diff --git a/Web/upload.gtpl b/Web/upload.gtpl new file mode 100644 index 0000000..18d2f1d --- /dev/null +++ b/Web/upload.gtpl @@ -0,0 +1,201 @@ + + + + + + 文件上传 + + + + + +
+

Pixel

+ +
+ +
+
+ + + +
+ +
+
+
+ +
+ 缩略图 +
+
+ + + + + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7d27c1c --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module Pixel + +go 1.21.3 + +require github.com/mattn/go-sqlite3 v1.14.19 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..3042612 --- /dev/null +++ b/go.sum @@ -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= diff --git a/main.go b/main.go new file mode 100644 index 0000000..f6cd9b3 --- /dev/null +++ b/main.go @@ -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) + } +} \ No newline at end of file