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:
parent
0176385654
commit
5f01299246
13
context.go
13
context.go
@ -4,7 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"net/http"
|
||||
"path"
|
||||
spath "path"
|
||||
|
||||
"fmt"
|
||||
|
||||
@ -193,12 +193,13 @@ func (c *Context) XML(code int, i interface{}) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// File sends a response with the content of the file. If attachment is true, the
|
||||
// client is prompted to save the file.
|
||||
func (c *Context) File(name string, attachment bool) (err error) {
|
||||
dir, file := path.Split(name)
|
||||
// File sends a response with the content of the file. If `attachment` is set
|
||||
// to true, the client is prompted to save the file with provided `name`,
|
||||
// name can be empty, in that case name of the file is used.
|
||||
func (c *Context) File(name, path string, attachment bool) (err error) {
|
||||
dir, file := spath.Split(path)
|
||||
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 {
|
||||
c.response.Header().Del(ContentDisposition)
|
||||
|
@ -141,7 +141,7 @@ func TestContext(t *testing.T) {
|
||||
// File
|
||||
rec = httptest.NewRecorder()
|
||||
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) {
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
assert.Equal(t, 219885, rec.Body.Len())
|
||||
@ -150,10 +150,10 @@ func TestContext(t *testing.T) {
|
||||
// File as attachment
|
||||
rec = httptest.NewRecorder()
|
||||
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) {
|
||||
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())
|
||||
}
|
||||
|
||||
|
@ -15,12 +15,12 @@
|
||||
</a>
|
||||
</li>
|
||||
<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>
|
||||
</a>
|
||||
</li>
|
||||
<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>
|
||||
</a>
|
||||
</li>
|
||||
|
Loading…
Reference in New Issue
Block a user