Add the file downloader.

This commit is contained in:
zicla
2018-07-03 13:45:22 +08:00
parent 1453d1a84b
commit b91374bdf3
5 changed files with 370 additions and 19 deletions

View File

@ -3,12 +3,12 @@ package rest
import (
"fmt"
"net/http"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"time"
"strings"
"net/url"
)
type AlienController struct {
@ -347,22 +347,21 @@ func (this *AlienController) Download(writer http.ResponseWriter, request *http.
this.PanicError(err)
defer diskFile.Close()
// 防止中文乱码
fileName := url.QueryEscape(matter.Name)
mimeType := GetMimeType(fileName)
writer.Header().Set("Content-Type", mimeType)
//如果是图片或者文本或者视频就直接打开。其余的一律以下载形式返回。
if strings.Index(mimeType, "image") != 0 && strings.Index(mimeType, "text") != 0 && strings.Index(mimeType, "video") != 0 {
writer.Header().Set("content-disposition", "attachment; filename=\""+fileName+"\"")
}
//对图片做缩放处理。
imageProcess := request.FormValue("imageProcess")
if imageProcess == "resize" {
this.matterService.ResizeImage(writer, request, matter, diskFile)
} else {
//如果是图片或者文本或者视频就直接打开。其余的一律以下载形式返回。
fileName := url.QueryEscape(matter.Name)
mimeType := GetMimeType(fileName)
if strings.Index(mimeType, "image") != 0 && strings.Index(mimeType, "text") != 0 && strings.Index(mimeType, "video") != 0 {
writer.Header().Set("content-disposition", "attachment; filename=\""+fileName+"\"")
}
this.matterService.DownloadFile(writer, request, matter, diskFile)
//显示文件大小。
//fileInfo, err := diskFile.Stat()
//if err != nil {