mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-24 19:09:57 +08:00
Add FileAttachment method to context to allow instant downloads with filenames
This commit is contained in:
parent
5d3f30cfc8
commit
d1b45be080
@ -6,6 +6,7 @@ package gin
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math"
|
"math"
|
||||||
@ -714,6 +715,13 @@ func (c *Context) File(filepath string) {
|
|||||||
http.ServeFile(c.Writer, c.Request, filepath)
|
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.
|
// SSEvent writes a Server-Sent Event into the body stream.
|
||||||
func (c *Context) SSEvent(name string, message interface{}) {
|
func (c *Context) SSEvent(name string, message interface{}) {
|
||||||
c.Render(-1, sse.Event{
|
c.Render(-1, sse.Event{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user