Listing
By default Iris will not list files and sub-directories when client requests a path of a directory (e.g. http://localhost:8080/files/folder). To enable file listing you just set DirOptions.ShowList to true:
options := iris.DirOptions{
// [...]
ShowList: true,
}
app.HandleDir("/files", iris.Dir("./uploads"), options)By default listing is rendering a simple page of <a href html links, like the standard net/http package does. To change the default behavior use the DirOptions.DirList field, which accepts a function of form:
type DirListFunc func(ctx iris.Context, dirOptions iris.DirOptions, dirName string, dir http.File) erroroptions := iris.DirOptions{
// [...]
ShowList: true,
DirList: func(ctx iris.Context, dirOptions iris.DirOptions, dirName string, dir http.File) error {
// [...]
},
}The DirListRich is a function helper for a better look & feel. It's a DirListFunc.
func DirListRich(opts ...DirListRichOptions) DirListFuncThe DirListRich function can optionally accept DirListRichOptions:
type DirListRichOptions struct {
// If not nil then this template's "dirlist" is used to render the listing page.
Tmpl *template.Template // * "html/template"
// If not empty then this view file is used to render the listing page.
// The view should be registered with `Application.RegisterView`.
// E.g. "dirlist.html"
TmplName string
}Usage
Example
Here's a full example that users can upload files and server can list them. It contains a customized listing page and delete file feature with basic authentication.
Create a main.go file and copy-paste the following contents:
The views/upload.html should look like that:
And finally the customized listing page. Copy-paste the following code to the views/dirlist.html file:
Last updated
Was this helpful?