feat(upload): 实现文件上传功能
- 添加处理 multipart 表单的 Go 函数 - 创建用于文件上传的 HTML 表单页面 - 支持解析和显示上传文件的元数据- 实现多文件上传处理逻辑 - 添加城市和姓名的文本文件示例- 集成 Bootstrap 样式提升界面美观度
This commit is contained in:
2
24-httpserver/httpserver/static/city.txt
Normal file
2
24-httpserver/httpserver/static/city.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
城市:重庆
|
||||
姓名:颜佐光
|
||||
31
24-httpserver/httpserver/static/upload.html
Normal file
31
24-httpserver/httpserver/static/upload.html
Normal file
@@ -0,0 +1,31 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"></meta>
|
||||
<title>Pro Go</title>
|
||||
<link href="bootstrap.min.css" rel="stylesheet"/>
|
||||
</head>
|
||||
<body>
|
||||
<div class="m-1 p-2 bg-primary text-white h2 text-center">
|
||||
Upload File
|
||||
</div>
|
||||
<form method="post" action="/forms/upload" class="p-2" enctype="multipart/form-data">
|
||||
<div class="form-group">
|
||||
<label class="form-label">Name</label>
|
||||
<input type="text" name="name" class="form-control"/>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">City</label>
|
||||
<input type="text" name="city" class="form-control"/>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">File</label>
|
||||
<input type="file" name="files" class="form-control" multiple/>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<button type="submit" class="btn btn-primary mt-2">Upload</button>
|
||||
</div>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
51
24-httpserver/httpserver/upload.go
Normal file
51
24-httpserver/httpserver/upload.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func HandleMultipartForm(writer http.ResponseWriter, request *http.Request) {
|
||||
fmt.Fprintf(writer, "Name: %v , City : %v \n", request.FormValue("name"), request.FormValue("city"))
|
||||
fmt.Fprintln(writer, "--------")
|
||||
|
||||
file, header, err := request.FormFile("files")
|
||||
if err != nil {
|
||||
http.Error(writer, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
fmt.Fprintf(writer, "File Name: %v, File Size: %v \n", header.Filename, header.Size)
|
||||
for k, v := range header.Header {
|
||||
fmt.Fprintf(writer, "Key: %v , Value: %v \n", k, v)
|
||||
}
|
||||
fmt.Fprintln(writer, "--------")
|
||||
io.Copy(writer, file)
|
||||
}
|
||||
|
||||
func HandleMultipartForm2(writer http.ResponseWriter, request *http.Request) {
|
||||
request.ParseMultipartForm(10 * 1024 * 1024)
|
||||
fmt.Fprintf(writer, "Name: %v , City : %v \n", request.FormValue("name"), request.FormValue("city"))
|
||||
fmt.Fprintln(writer, "--------\n")
|
||||
|
||||
for _, header := range request.MultipartForm.File["files"] {
|
||||
fmt.Fprintf(writer, "\n\n--------\nFile Name: %v, File Size: %v \n", header.Filename, header.Size)
|
||||
file, err := header.Open()
|
||||
if err != nil {
|
||||
http.Error(writer, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
for k, v := range header.Header {
|
||||
fmt.Fprintf(writer, "Key: %v , Value: %v \n", k, v)
|
||||
}
|
||||
fmt.Fprintln(writer, "--------")
|
||||
io.Copy(writer, file)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func init() {
|
||||
http.HandleFunc("/forms/upload", HandleMultipartForm2)
|
||||
}
|
||||
Reference in New Issue
Block a user