Add filename to save file in the browser

This commit is contained in:
anatoly@nspcc.ru 2020-06-10 20:04:27 +03:00
parent 799d391508
commit 793d21651b

View file

@ -113,6 +113,7 @@ func (a *app) receiveFile(c *fasthttp.RequestCtx) {
func receiveObject(c *fasthttp.RequestCtx, cli object.Service_GetClient) error {
var (
typ string
content_disp_type string
put = c.Request.URI().QueryArgs().GetBool("download")
)
@ -138,9 +139,12 @@ func receiveObject(c *fasthttp.RequestCtx, cli object.Service_GetClient) error {
if hdr := obj.Headers[i].GetUserHeader(); hdr != nil {
c.Response.Header.Set("x-"+hdr.Key, hdr.Value)
if hdr.Key == object.FilenameHeader && put {
if hdr.Key == object.FilenameHeader {
if put {
content_disp_type = "attachment"
} else { content_disp_type = "inline"}
// NOTE: we use path.Base because hdr.Value can be something like `/path/to/filename.ext`
c.Response.Header.Set("Content-Disposition", "attachment; filename="+path.Base(hdr.Value))
c.Response.Header.Set("Content-Disposition", content_disp_type+"; filename="+path.Base(hdr.Value))
}
}
}