Compare commits

...

4 Commits

Author SHA1 Message Date
junleea d33597766a download file 2024-08-30 15:47:57 +08:00
junleea 9e5ca5e897 download file 2024-08-30 15:41:51 +08:00
junleea 5228a4270e download file 2024-08-30 15:40:45 +08:00
junleea a295d750cc upload type 2024-08-30 15:28:39 +08:00
1 changed files with 25 additions and 3 deletions

View File

@ -1,8 +1,11 @@
package handler
import (
"fmt"
"github.com/gin-gonic/gin"
"io"
"net/http"
"os"
"strconv"
"videoplayer/dao"
"videoplayer/proto"
@ -75,13 +78,32 @@ func DownloadFile(c *gin.Context) {
id, _ := c.Get("id")
//查询文件信息
//file := dao.FindFileByNames(file_id, int(id.(float64)))
file := dao.FindFileByID(file_id, int(id.(float64)))
if file.ID == 0 {
file_ := dao.FindFileByID(file_id, int(id.(float64)))
if file_.ID == 0 {
c.JSON(http.StatusOK, gin.H{"error": "file not found", "code": proto.FileNotFound, "message": "failed"})
return
}
//下载文件
c.File(file.FilePath + file.FileStoreName)
// 打开文件
file, err := os.Open(file_.FilePath + "/" + file_.FileStoreName)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error", "message": "Failed to open file"})
return
}
defer file.Close()
// 设置响应头
c.Writer.Header().Set("Content-Type", "application/octet-stream")
c.Writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", file_.FileName))
// 发送文件内容
_, err = io.Copy(c.Writer, file)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error", "message": "Failed to send file"})
return
}
c.Status(http.StatusOK)
}
func SetRedis(c *gin.Context) {