3 Star 2 Fork 1

fotomxq/weeekj_core

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
download_local_file.go 1.56 KB
一键复制 编辑 原始数据 按行查看 历史
fotomxq 提交于 2025-02-05 18:52 +08:00 . 修改项目路径;
package Router2File
import (
CoreFile "gitee.com/fotomxq/weeekj_core/v5/core/file"
CoreFilter "gitee.com/fotomxq/weeekj_core/v5/core/filter"
Router2Mid "gitee.com/fotomxq/weeekj_core/v5/router2/mid"
Router2SystemConfig "gitee.com/fotomxq/weeekj_core/v5/router2/system_config"
"net/http"
)
// DownloadLocalFile 下载本地文件
func DownloadLocalFile(c *Router2Mid.RouterURLPublicC) {
//获取目录
tempChildDir := c.Context.Param("dir")
if tempChildDir == "" {
Router2Mid.ReportBaseError(c, "report_params_lost")
return
}
//获取文件名称
tempFileName := c.Context.Param("temp_file")
if tempFileName == "" {
Router2Mid.ReportBaseError(c, "report_params_lost")
return
}
//检查temp合法性和文件是否存在
if !CoreFilter.CheckFileName(tempChildDir) || !CoreFilter.CheckFileName(tempFileName) {
Router2Mid.ReportBaseError(c, "report_params_error")
return
}
tempFile := Router2SystemConfig.RootDir + CoreFile.Sep + "temp" + CoreFile.Sep + tempChildDir + CoreFile.Sep + tempFileName
if b := CoreFile.IsFile(tempFile); !b {
Router2Mid.ReportWarnLog(c, "download local file,temp file:"+tempFile, nil, "err_io")
return
}
//下载文件
fileData, err := CoreFile.LoadFile(tempFile)
if err != nil {
Router2Mid.ReportWarnLog(c, "download local file", err, "err_io")
return
}
fileContentDisposition := "attachment;filename=\"" + tempFileName + "\""
c.Context.Header("Content-Type", "application/octet-stream")
c.Context.Header("Content-Disposition", fileContentDisposition)
c.Context.Data(http.StatusOK, "application/octet-stream", fileData)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/fotomxq/weeekj_core.git
git@gitee.com:fotomxq/weeekj_core.git
fotomxq
weeekj_core
weeekj_core
v5.4.25

搜索帮助