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

Context.File now accepts name of attachment

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-10-11 11:43:27 -07:00
parent 0176385654
commit 5f01299246
3 changed files with 12 additions and 11 deletions

View File

@ -4,7 +4,7 @@ import (
"encoding/json" "encoding/json"
"encoding/xml" "encoding/xml"
"net/http" "net/http"
"path" spath "path"
"fmt" "fmt"
@ -193,12 +193,13 @@ func (c *Context) XML(code int, i interface{}) (err error) {
return return
} }
// File sends a response with the content of the file. If attachment is true, the // File sends a response with the content of the file. If `attachment` is set
// client is prompted to save the file. // to true, the client is prompted to save the file with provided `name`,
func (c *Context) File(name string, attachment bool) (err error) { // name can be empty, in that case name of the file is used.
dir, file := path.Split(name) func (c *Context) File(name, path string, attachment bool) (err error) {
dir, file := spath.Split(path)
if attachment { if attachment {
c.response.Header().Set(ContentDisposition, "attachment; filename="+file) c.response.Header().Set(ContentDisposition, "attachment; filename="+name)
} }
if err = serveFile(dir, file, c); err != nil { if err = serveFile(dir, file, c); err != nil {
c.response.Header().Del(ContentDisposition) c.response.Header().Del(ContentDisposition)

View File

@ -141,7 +141,7 @@ 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", false) 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())
@ -150,10 +150,10 @@ func TestContext(t *testing.T) {
// File as attachment // File as attachment
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", true) err = c.File("WALLE.PNG", "test/fixture/walle.png", true)
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, rec.Header().Get(ContentDisposition), "attachment; filename=walle.png") assert.Equal(t, rec.Header().Get(ContentDisposition), "attachment; filename=WALLE.PNG")
assert.Equal(t, 219885, rec.Body.Len()) assert.Equal(t, 219885, rec.Body.Len())
} }

View File

@ -15,12 +15,12 @@
</a> </a>
</li> </li>
<li class="twitter"> <li class="twitter">
<a href="http://twitter.com/labstack" target="_blank"> <a href="https://twitter.com/labstack" target="_blank">
<i class="fa fa-twitter-square fa-2x"></i> <i class="fa fa-twitter-square fa-2x"></i>
</a> </a>
</li> </li>
<li class="google"> <li class="google">
<a href="http://plus.google.com/+labstack" target="_blank"> <a href="https://plus.google.com/+labstack" target="_blank">
<i class="fa fa-google-plus-square fa-2x"></i> <i class="fa fa-google-plus-square fa-2x"></i>
</a> </a>
</li> </li>