mirror of
https://github.com/labstack/echo.git
synced 2025-01-24 03:16:14 +02:00
Merge branch 'master' of https://github.com/labstack/echo
This commit is contained in:
commit
4187265fa4
15
context.go
15
context.go
@ -3,6 +3,7 @@ package echo
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"mime"
|
"mime"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
@ -140,6 +141,10 @@ type (
|
|||||||
// client to save the file.
|
// client to save the file.
|
||||||
Attachment(io.ReadSeeker, string) error
|
Attachment(io.ReadSeeker, string) error
|
||||||
|
|
||||||
|
// Inline sends a response from `io.ReaderSeeker` as inline, opening
|
||||||
|
// the file in the browser.
|
||||||
|
Inline(io.ReadSeeker, string) error
|
||||||
|
|
||||||
// NoContent sends a response with no body and a status code.
|
// NoContent sends a response with no body and a status code.
|
||||||
NoContent(int) error
|
NoContent(int) error
|
||||||
|
|
||||||
@ -417,8 +422,16 @@ func (c *echoContext) File(file string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *echoContext) Attachment(r io.ReadSeeker, name string) (err error) {
|
func (c *echoContext) Attachment(r io.ReadSeeker, name string) (err error) {
|
||||||
|
return c.contentDisposition(r, name, "attachment")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *echoContext) Inline(r io.ReadSeeker, name string) (err error) {
|
||||||
|
return c.contentDisposition(r, name, "inline")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *echoContext) contentDisposition(r io.ReadSeeker, name, dispositionType string) (err error) {
|
||||||
c.response.Header().Set(HeaderContentType, ContentTypeByExtension(name))
|
c.response.Header().Set(HeaderContentType, ContentTypeByExtension(name))
|
||||||
c.response.Header().Set(HeaderContentDisposition, "attachment; filename="+name)
|
c.response.Header().Set(HeaderContentDisposition, fmt.Sprintf("%s; filename=%s", dispositionType, name))
|
||||||
c.response.WriteHeader(http.StatusOK)
|
c.response.WriteHeader(http.StatusOK)
|
||||||
_, err = io.Copy(c.response, r)
|
_, err = io.Copy(c.response, r)
|
||||||
return
|
return
|
||||||
|
@ -145,6 +145,19 @@ func TestContext(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Inline
|
||||||
|
rec = test.NewResponseRecorder()
|
||||||
|
c = e.NewContext(req, rec).(*echoContext)
|
||||||
|
file, err = os.Open("_fixture/images/walle.png")
|
||||||
|
if assert.NoError(t, err) {
|
||||||
|
err = c.Inline(file, "walle.png")
|
||||||
|
if assert.NoError(t, err) {
|
||||||
|
assert.Equal(t, http.StatusOK, rec.Status())
|
||||||
|
assert.Equal(t, "inline; filename=walle.png", rec.Header().Get(HeaderContentDisposition))
|
||||||
|
assert.Equal(t, 219885, rec.Body.Len())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// NoContent
|
// NoContent
|
||||||
rec = test.NewResponseRecorder()
|
rec = test.NewResponseRecorder()
|
||||||
c = e.NewContext(req, rec).(*echoContext)
|
c = e.NewContext(req, rec).(*echoContext)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user