Add FileAttachment method to context to allow instant downloads with filenames

This commit is contained in:
Emman 2018-02-28 01:04:49 +08:00
parent 5d3f30cfc8
commit d1b45be080

View File

@ -6,6 +6,7 @@ package gin
import (
"errors"
"fmt"
"io"
"io/ioutil"
"math"
@ -714,6 +715,13 @@ func (c *Context) File(filepath string) {
http.ServeFile(c.Writer, c.Request, filepath)
}
// FileAttachment writes the specified file into the body stream in an efficient way
// On the client side, the file will typically be downloaded with the given filename
func (c *Context) FileAttachment(filepath, filename string) {
c.Writer.Header().Set("content-disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename))
http.ServeFile(c.Writer, c.Request, filepath)
}
// SSEvent writes a Server-Sent Event into the body stream.
func (c *Context) SSEvent(name string, message interface{}) {
c.Render(-1, sse.Event{