Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add direct download option when serving static files #1729

Merged
merged 2 commits into from Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions app.go
Expand Up @@ -374,6 +374,10 @@ type Static struct {
// Optional. Default value false.
Browse bool `json:"browse"`

// When set to true, enables direct download.
// Optional. Default value false.
Download bool `json:"download"`

// The name of the index file for serving a directory.
// Optional. Default value "index.html".
Index string `json:"index"`
Expand Down
4 changes: 4 additions & 0 deletions router.go
Expand Up @@ -375,6 +375,10 @@ func (app *App) registerStatic(prefix, root string, config ...Static) Router {
}
// Serve file
fileHandler(c.fasthttp)
// Sets the response Content-Disposition header to attachment if the Download option is true
if len(config) > 0 && config[0].Download {
c.Attachment()
}
// Return request if found and not forbidden
status := c.fasthttp.Response.StatusCode()
if status != StatusNotFound && status != StatusForbidden {
Expand Down