golangGolang语言社区Go

Go Gin 实现文件的上传下载流读取

2020-08-06  本文已影响0人  五岁小孩

Go Gin 实现文件的上传下载流读取

文件上传

文件的下载

文件流读取

//map for  Http Content-Type  Http 文件类型对应的content-Type
var HttpContentType = map[string]string{
  ".avi": "video/avi",
  ".mp3": "   audio/mp3",
  ".mp4": "video/mp4",
  ".wmv": "   video/x-ms-wmv",
  ".asf":  "video/x-ms-asf",
  ".rm":   "application/vnd.rn-realmedia",
  ".rmvb": "application/vnd.rn-realmedia-vbr",
  ".mov":  "video/quicktime",
  ".m4v":  "video/mp4",
  ".flv":  "video/x-flv",
  ".jpg":  "image/jpeg",
  ".png":  "image/png",
}

//根据文件路径读取返回流文件 参数url
func PubResFileStreamGetService(c *gin.Context) {
filePath := c.Query("url")
//获取文件名称带后缀
fileNameWithSuffix := path.Base(filePath)
//获取文件的后缀
fileType := path.Ext(fileNameWithSuffix)
//获取文件类型对应的http ContentType 类型
fileContentType := HttpContentType[fileType]
if common.IsEmpty(fileContentType) {
  c.String(http.StatusNotFound, "file http contentType not found")
  return
}
c.Header("Content-Type", fileContentType)
c.File(filePath)
}
上一篇 下一篇

猜你喜欢

热点阅读