进阶 www.iris-go.com 2026-10-20 02:39:14 · 22 阅读
第9章 Iris 框架目录列表功能详解
# 列表
默认情况下,当客户端请求一个目录路径时(例如 http://localhost:8080/files/folder),Iris 不会列出其中的文件和子目录。要启用目录列表功能,只需把 DirOptions.ShowList 设为 true:
```go
options := iris.DirOptions{
// [...]
ShowList: true,
}
app.HandleDir("/files", iris.Dir("./uploads"), options)
```
默认的列表页面只渲染简单的 `= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB",
float64(b)/float64(div), "kMGTPE"[exp])
})
app.RegisterView(view)
// Serve assets (e.g. javascript, css).
// app.HandleDir("/public", "./public")
app.Get("/", index)
app.Get("/upload", uploadView)
app.Post("/upload", upload)
filesRouter := app.Party("/files")
{
filesRouter.HandleDir("/", iris.Dir(uploadDir), iris.DirOptions{
Compress: true,
ShowList: true,
DirList: iris.DirListRich(iris.DirListRichOptions{
// Optionally, use a custom template for listing:
// Tmpl: dirListRichTemplate,
TmplName: "dirlist.html",
}),
})
auth := basicauth.Default(map[string]string{
"myusername": "mypassword",
})
filesRouter.Delete("/{file:path}", auth, deleteFile)
}
app.Listen(":8080")
}
func index(ctx iris.Context) {
ctx.Redirect("/upload")
}
func uploadView(ctx iris.Context) {
now := time.Now().Unix()
h := md5.New()
io.WriteString(h, strconv.FormatInt(now, 10))
token := fmt.Sprintf("%x", h.Sum(nil))
ctx.View("upload.html", token)
}
func upload(ctx iris.Context) {
ctx.SetMaxRequestBodySize(maxSize)
_, _, err := ctx.UploadFormFiles(uploadDir, beforeSave)
if err != nil {
ctx.StopWithError(iris.StatusPayloadTooRage, err)
return
}
ctx.Redirect("/files")
}
func beforeSave(ctx iris.Context, file *multipart.FileHeader) {
ip := ctx.RemoteAddr()
ip = strings.ReplaceAll(ip, ".", "_")
ip = strings.ReplaceAll(ip, ":", "_")
file.Filename = ip + "-" + file.Filename
}
func deleteFile(ctx iris.Context) {
// It does not contain the system path,
// as we are not exposing it to the user.
fileName := ctx.Params().Get("file")
filePath := path.Join(uploadDir, fileName)
if err := os.RemoveAll(filePath); err != nil {
ctx.StopWithError(iris.StatusInternalServerError, err)
return
}
ctx.Redirect("/files")
}
```
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
views/upload.html 文件内容应如下所示:html上传文件
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
最后是定制好的目录列表页面。将下面的代码复制粘贴到 views/dirlist.html 文件中: ```html{{.Title}}
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
views/upload.html 文件内容应如下所示:html
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
最后是定制好的目录列表页面。将下面的代码复制粘贴到 views/dirlist.html 文件中: ```html
| # | 名称 | 大小 | 操作 | |
|---|---|---|---|---|
| {{ $idx }} | {{ $file.Name }} | {{ if $file.Info.IsDir }}目录 | {{ else }}{{ formatBytes $file.Info.Size }} | ``` ``` {{ end }}
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121