diff --git a/context.go b/context.go index d9e66f36a..63ac242ab 100644 --- a/context.go +++ b/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 diff --git a/context_test.go b/context_test.go index 9c23759c0..4f47a2ac7 100644 --- a/context_test.go +++ b/context_test.go @@ -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)