mirror of
https://github.com/labstack/echo.git
synced 2025-01-24 03:16:14 +02:00
Inline feature (#637)
This commit is contained in:
parent
6fd725c953
commit
8a85626a71
15
context.go
15
context.go
@ -3,6 +3,7 @@ package echo
|
||||
import (
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime"
|
||||
"mime/multipart"
|
||||
@ -140,6 +141,10 @@ type (
|
||||
// client to save the file.
|
||||
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(int) error
|
||||
|
||||
@ -417,8 +422,16 @@ func (c *echoContext) File(file string) 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(HeaderContentDisposition, "attachment; filename="+name)
|
||||
c.response.Header().Set(HeaderContentDisposition, fmt.Sprintf("%s; filename=%s", dispositionType, name))
|
||||
c.response.WriteHeader(http.StatusOK)
|
||||
_, err = io.Copy(c.response, r)
|
||||
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
|
||||
rec = test.NewResponseRecorder()
|
||||
c = e.NewContext(req, rec).(*echoContext)
|
||||
|
Loading…
x
Reference in New Issue
Block a user