1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-26 09:51:28 +00:00

Merge pull request #1827 from ysqi/issue

Check file before download
This commit is contained in:
astaxie 2016-03-24 13:33:44 +08:00
commit 2362ca00b5

View File

@ -24,6 +24,7 @@ import (
"io" "io"
"mime" "mime"
"net/http" "net/http"
"os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
@ -237,6 +238,12 @@ func (output *BeegoOutput) XML(data interface{}, hasIndent bool) error {
// Download forces response for download file. // Download forces response for download file.
// it prepares the download response header automatically. // it prepares the download response header automatically.
func (output *BeegoOutput) Download(file string, filename ...string) { func (output *BeegoOutput) Download(file string, filename ...string) {
// check get file error, file not found or other error.
if _, err := os.Stat(file); err != nil {
http.ServeFile(output.Context.ResponseWriter, output.Context.Request, file)
return
}
output.Header("Content-Description", "File Transfer") output.Header("Content-Description", "File Transfer")
output.Header("Content-Type", "application/octet-stream") output.Header("Content-Type", "application/octet-stream")
if len(filename) > 0 && filename[0] != "" { if len(filename) > 0 && filename[0] != "" {