|
@@ -978,19 +978,37 @@ func (this *ResourceAuthController) FileDownload() {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+ // 生成本地文件
|
|
|
+ localFilePath := fmt.Sprintf("%s%s", utils.GetRandStringNoSpecialChar(6), fileName)
|
|
|
+ localFile, e := os.Create(localFilePath)
|
|
|
+ if e != nil {
|
|
|
+ br.Msg = "下载失败"
|
|
|
+ br.ErrMsg = "生成本地文件失败, Err: " + e.Error()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer func() {
|
|
|
+ if e = localFile.Close(); e != nil {
|
|
|
+ fmt.Println("local file close err: ", e.Error())
|
|
|
+ }
|
|
|
+ if e = os.Remove(localFilePath); e != nil {
|
|
|
+ fmt.Println("local file remove err: ", e.Error())
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
// 写入响应流
|
|
|
- _, e = io.Copy(this.Ctx.ResponseWriter, down.Body)
|
|
|
+ //_, e = io.Copy(this.Ctx.ResponseWriter, down.Body)
|
|
|
+ _, e = io.Copy(localFile, down.Body)
|
|
|
if e != nil {
|
|
|
br.Msg = "下载失败"
|
|
|
br.ErrMsg = "复制文件资源失败, Err: " + e.Error()
|
|
|
return
|
|
|
}
|
|
|
-
|
|
|
// 设置响应头
|
|
|
- this.Ctx.ResponseWriter.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", fileName))
|
|
|
- this.Ctx.ResponseWriter.Header().Set("Content-Type", "application/octet-stream")
|
|
|
+ //this.Ctx.ResponseWriter.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", fileName))
|
|
|
+ //this.Ctx.ResponseWriter.Header().Set("Content-Type", "application/octet-stream")
|
|
|
|
|
|
br.Ret = 200
|
|
|
br.Msg = "下载成功"
|
|
|
br.Success = true
|
|
|
+ this.Ctx.Output.Download(localFilePath, fileName)
|
|
|
}
|