diff --git a/context.go b/context.go index fe1386ac..df1134d7 100644 --- a/context.go +++ b/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) diff --git a/context_test.go b/context_test.go index 8d9becaf..1f258a67 100644 --- a/context_test.go +++ b/context_test.go @@ -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()) } diff --git a/website/layouts/partials/footer.html b/website/layouts/partials/footer.html index 23e950cd..77a8c07c 100644 --- a/website/layouts/partials/footer.html +++ b/website/layouts/partials/footer.html @@ -15,12 +15,12 @@