1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

Merge pull request #185 from labstack/context-file

Added attachment as option in Context.File function
This commit is contained in:
Vishal Rana 2015-08-24 22:04:08 -07:00
commit 2450450a07
3 changed files with 28 additions and 13 deletions

View File

@ -3,15 +3,13 @@ package echo
import ( import (
"encoding/json" "encoding/json"
"encoding/xml" "encoding/xml"
"io"
"net/http" "net/http"
"path"
"fmt" "fmt"
"net/url" "net/url"
"os"
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
) )
@ -166,16 +164,14 @@ func (c *Context) XML(code int, i interface{}) error {
return xml.NewEncoder(c.response).Encode(i) return xml.NewEncoder(c.response).Encode(i)
} }
// File sends a file as attachment. // File sends a response with the content of the file. If attachment is true, the
func (c *Context) File(name string) error { // client is prompted to save the file.
file, err := os.Open(name) func (c *Context) File(name string, attachment bool) error {
if err != nil { dir, file := path.Split(name)
return err if attachment {
c.response.Header().Set(ContentDisposition, "attachment; filename="+file)
} }
fi, _ := file.Stat() return serveFile(dir, file, c)
c.response.Header().Set(ContentDisposition, "attachment; filename="+fi.Name())
_, err = io.Copy(c.response, file)
return err
} }
// NoContent sends a response with no body and a status code. // NoContent sends a response with no body and a status code.

View File

@ -141,12 +141,22 @@ func TestContext(t *testing.T) {
// File // File
rec = httptest.NewRecorder() rec = httptest.NewRecorder()
c = NewContext(req, NewResponse(rec), New()) c = NewContext(req, NewResponse(rec), New())
err = c.File("test/fixture/walle.png") err = c.File("test/fixture/walle.png", false)
if assert.NoError(t, err) { if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, 219885, rec.Body.Len()) assert.Equal(t, 219885, rec.Body.Len())
} }
// File as attachment
rec = httptest.NewRecorder()
c = NewContext(req, NewResponse(rec), New())
err = c.File("test/fixture/walle.png", true)
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, rec.Header().Get(ContentDisposition), "attachment; filename=walle.png")
assert.Equal(t, 219885, rec.Body.Len())
}
// NoContent // NoContent
rec = httptest.NewRecorder() rec = httptest.NewRecorder()
c = NewContext(req, NewResponse(rec), New()) c = NewContext(req, NewResponse(rec), New())

View File

@ -368,6 +368,15 @@ Context.String(code int, s string) error
Sends a text/plain HTTP response with status code. Sends a text/plain HTTP response with status code.
### File
```go
Context.File(name string, attachment bool) error
```
File sends a response with the content of the file. If attachment is true, the client
is prompted to save the file.
### Static files ### Static files
`Echo.Static(path, root string)` serves static files. For example, code below serves `Echo.Static(path, root string)` serves static files. For example, code below serves