不为别的,只为做一个连自己都羡慕的人

go+gin实现文件预览

r := gin.Default()
//Get路由,动态路由
r.GET("/GetFile/:name", DowFile)
// DowFile 文件在线预览
func DowFile(c *gin.Context){
//通过动态路由方式获取文件名,以实现下载不同文件的功能
name:=c.Param("name")
uploadType := c.Query("type")

fmt.Println(name)
var uploadT = ""
if uploadType =="1" {
uploadT ="teaching"
}else {
uploadT ="professional"
}
fmt.Println(uploadType)
var localPath = "./dataset/"+uploadT+"/"+name
fmt.Println(localPath)
err:=minioTest.DownloadObject(uploadT, name, localPath)
fmt.Println("**************")
fmt.Println(err)
if err =="error"{
c.HTML(http.StatusOK, "error.html", gin.H{
"title": "error",
})
}else{
c.Header("Content-Type", "application/octet-stream")
c.Header("Content-Disposition", "attachment; filename="+name)
c.Header("Content-Transfer-Encoding", "binary")
//拼接路径,如果没有这一步,则默认在当前路径下寻找
//filename:=path.Join("./dataset/professional/",name)
filename:=path.Join("./dataset/"+uploadT+"/",name)
//响应一个文件
c.File(filename)
return
}

}

 

posted @ 2022-01-25 17:33  升级打怪  阅读(668)  评论(0编辑  收藏  举报