Go Gin 文件上传下载

发布时间 2023-07-05 14:35:46作者: snake_j
//判断文件是否存在  存在返回 true 不存在返回false                                                                                                                         
// func checkFileIsExist(filename string) bool {                                                                                                                          
//  var exist = true                                                                                                                                                      
//  if _, err := os.Stat(filename); os.IsNotExist(err) {                                                                                                                  
//      exist = false                                                                                                                                                     
//  }                                                                                                                                                                     
//  return exist                                                                                                                                                          
// }                                                                                                                                                                      

func UpFile(c *gin.Context){
    // 为 multipart forms 设置较低的内存限制 (默认是 32 MiB)                                                                                                              
    // router.MaxMultipartMemory = 8 << 20  // 8 MiB                                                                                                                      

    // 单文件                                                                                                                                                             
    file, _ := c.FormFile("file")
    log.Println(file.Filename)

    // 上传文件至指定目录                                                                                                                                                 

    if err := c.SaveUploadedFile(file, `./upload/` + file.Filename); err != nil {
        c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))
        return
    }

    c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename))
}

func FileExist(path string) bool {
    _, err := os.Lstat(path)
    return !os.IsNotExist(err)
}

func DownFile(c *gin.Context){
    downfile := c.Param("downloadfile")

    filename := "周报-202054-510.xls"

    dfile := fmt.Sprintf("/data/go/src/goWEB/upload/%s",downfile)

    if fret := FileExist(dfile) ; fret {
        c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))//fmt.Sprintf("attachment; filename=%s", filename)对下载的文件重命名
        c.Writer.Header().Add("Content-Type", "application/octet-stream")
        c.File(dfile)
    } else {
        c.JSON(http.StatusOK, fmt.Sprintf("'%s' is not exits", dfile))
        return
    }
}