Skip to content

Commit 5f01299

Browse files
committed
Context.File now accepts name of attachment
Signed-off-by: Vishal Rana <vr@labstack.com>
1 parent 0176385 commit 5f01299

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

context.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"encoding/json"
55
"encoding/xml"
66
"net/http"
7-
"path"
7+
spath "path"
88

99
"fmt"
1010

@@ -193,12 +193,13 @@ func (c *Context) XML(code int, i interface{}) (err error) {
193193
return
194194
}
195195

196-
// File sends a response with the content of the file. If attachment is true, the
197-
// client is prompted to save the file.
198-
func (c *Context) File(name string, attachment bool) (err error) {
199-
dir, file := path.Split(name)
196+
// File sends a response with the content of the file. If `attachment` is set
197+
// to true, the client is prompted to save the file with provided `name`,
198+
// name can be empty, in that case name of the file is used.
199+
func (c *Context) File(name, path string, attachment bool) (err error) {
200+
dir, file := spath.Split(path)
200201
if attachment {
201-
c.response.Header().Set(ContentDisposition, "attachment; filename="+file)
202+
c.response.Header().Set(ContentDisposition, "attachment; filename="+name)
202203
}
203204
if err = serveFile(dir, file, c); err != nil {
204205
c.response.Header().Del(ContentDisposition)

context_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func TestContext(t *testing.T) {
141141
// File
142142
rec = httptest.NewRecorder()
143143
c = NewContext(req, NewResponse(rec), New())
144-
err = c.File("test/fixture/walle.png", false)
144+
err = c.File("", "test/fixture/walle.png", false)
145145
if assert.NoError(t, err) {
146146
assert.Equal(t, http.StatusOK, rec.Code)
147147
assert.Equal(t, 219885, rec.Body.Len())
@@ -150,10 +150,10 @@ func TestContext(t *testing.T) {
150150
// File as attachment
151151
rec = httptest.NewRecorder()
152152
c = NewContext(req, NewResponse(rec), New())
153-
err = c.File("test/fixture/walle.png", true)
153+
err = c.File("WALLE.PNG", "test/fixture/walle.png", true)
154154
if assert.NoError(t, err) {
155155
assert.Equal(t, http.StatusOK, rec.Code)
156-
assert.Equal(t, rec.Header().Get(ContentDisposition), "attachment; filename=walle.png")
156+
assert.Equal(t, rec.Header().Get(ContentDisposition), "attachment; filename=WALLE.PNG")
157157
assert.Equal(t, 219885, rec.Body.Len())
158158
}
159159

website/layouts/partials/footer.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
</a>
1616
</li>
1717
<li class="twitter">
18-
<a href="http://twitter.com/labstack" target="_blank">
18+
<a href="https://twitter.com/labstack" target="_blank">
1919
<i class="fa fa-twitter-square fa-2x"></i>
2020
</a>
2121
</li>
2222
<li class="google">
23-
<a href="http://plus.google.com/+labstack" target="_blank">
23+
<a href="https://plus.google.com/+labstack" target="_blank">
2424
<i class="fa fa-google-plus-square fa-2x"></i>
2525
</a>
2626
</li>

0 commit comments

Comments
 (0)