-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
65 lines (52 loc) · 1.52 KB
/
main.go
File metadata and controls
65 lines (52 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"fmt"
"net/http"
"path/filepath"
"github.com/gin-gonic/gin"
)
func main() {
port := 8080
filePath := "/data/data/uploader"
// enableAuth := false
// username := "admin"
// password := "admin"
router := gin.Default()
// 文件下载
router.StaticFS("/", http.Dir(filePath))
// 单文件上传
router.POST("/upload", func(c *gin.Context) {
// 获取上传的文件
file, err := c.FormFile("file")
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "上传文件失败"})
return
}
// 设置保存路径
dst := filepath.Join(filePath, file.Filename)
// 保存文件到服务器
if err := c.SaveUploadedFile(file, dst); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "保存文件失败"})
return
}
// 返回成功信息
c.JSON(http.StatusOK, gin.H{"message": "上传成功", "filename": file.Filename})
})
// 多文件上传
router.POST("/uploads", func(c *gin.Context) {
form, _ := c.MultipartForm()
files := form.File["files"] // 获取多个文件
var uploadedFiles []string
for _, file := range files {
dst := filepath.Join(filePath, file.Filename)
if err := c.SaveUploadedFile(file, dst); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "保存文件失败"})
return
}
uploadedFiles = append(uploadedFiles, file.Filename)
}
c.JSON(http.StatusOK, gin.H{"message": "上传成功", "files": uploadedFiles})
})
router.Run(":" + fmt.Sprint(port))
fmt.Println("Server started on port", port)
}