11.上传文件
# 01.上传文件
# 1.1 上传文件页面
views/upload.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>upload page</h1>
<form enctype="multipart/form-data" method="post" action="/upload/doUpload">
标题: <input type="text" name="title" />
<br><br>
文件:<input type="file" name="pic" />
<br><br>
<input type="submit" value="提交">
</form>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 1.2 控制器
controllers\upload.go
Beego 提供了两个很方便的方法来处理文件上传:
GetFile(key string) (multipart.File, *multipart.FileHeader, error)
- 该方法主要用于用户读取表单中的文件名 the_file,然后返回相应的信息
- 用户根据这些变量来处理文件上传:过滤、保存文件等。
SaveToFile(fromfile, tofile string) error
- 该方法是在 GetFile 的基础上实现了快速保存的功能fromfile
- 是提交时候的 html 表单中的 name
package controllers
import (
"github.com/astaxie/beego"
)
type UploadController struct {
beego.Controller
}
func (c *UploadController) UploadPage() {
c.TplName = "upload.html"
}
func (c *UploadController) DoUpload() {
title := c.GetString("title")
beego.Info(title)
f, h, err := c.GetFile("pic")
if err != nil {
beego.Info("getfile err ", err)
}
defer f.Close()
c.SaveToFile("pic", "static/upload/"+h.Filename) // 保存位置在 static/upload, 没有文 件夹要先创建
c.Ctx.WriteString("上传成功")
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 1.3 路由
func init() {
beego.Router("/upload/page", &controllers.UploadController{}, "get:UploadPage")
beego.Router("/upload/doUpload", &controllers.UploadController{}, "post:DoUpload")
}
1
2
3
4
2
3
4
# 02.根据日期生成存储目录
上传类型判断、以及根据日期生成存储目录
# 1.1 上传文件页面
views/upload.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>upload page</h1>
<form enctype="multipart/form-data" method="post" action="/upload/doUpload">
标题: <input type="text" name="title" />
<br><br>
文件:<input type="file" name="pic" />
<br><br>
<input type="submit" value="提交">
</form>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 1.2 控制器
package controllers
import (
"beegodemo02/models"
"github.com/astaxie/beego"
"os"
"path"
"strconv"
)
type UploadController struct {
beego.Controller
}
func (c *UploadController) UploadPage() {
c.TplName = "upload.html"
}
func (c *UploadController) DoUpload() {
title := c.GetString("title")
beego.Info(title)
//1、获取上传的文件
f, h, err := c.GetFile("pic")
if err != nil {
beego.Error(err)
return
}
//2、关闭文件流
defer f.Close()
//3、获取后缀名 判断类型是否正确 .jpg .png .gif .jpeg
extName := path.Ext(h.Filename)
allowExtMap := map[string]bool{
".jpg": true,
".png": true,
".gif": true,
".jpeg": true,
}
if _, ok := allowExtMap[extName]; !ok {
c.Ctx.WriteString("文件类型不合法")
return
}
//4、创建图片保存目录 static/upload/20200623
day := models.GetDay()
dir := "static/upload/" + day
if err := os.MkdirAll(dir, 0666); err != nil {
beego.Error(err)
}
//5、生成文件名称 144325235235.png
fileUnixName := strconv.FormatInt(models.GetUnix(), 10)
//static/upload/20200623/144325235235.png
saveDir := path.Join(dir, fileUnixName+extName)
//6、保存图片
c.SaveToFile("pic", saveDir)
c.Ctx.WriteString("上传图片成功")
}
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
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
# 1.3 路由
func init() {
beego.Router("/upload/page", &controllers.UploadController{}, "get:UploadPage")
beego.Router("/upload/doUpload", &controllers.UploadController{}, "post:DoUpload")
}
1
2
3
4
2
3
4
# 1.4 公共方法
models\tools.go
package models
import (
"time"
)
func GetUnix() int64 {
return time.Now().Unix()
}
func GetDay() string {
template := "20060102"
return time.Now().Format(template)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 1.5 上传结果
上次更新: 2024/3/13 15:35:10