首页 > 其他分享 >Go Gin 文件上传下载

Go Gin 文件上传下载

时间:2023-07-05 14:46:26浏览次数:34  
标签:false 上传下载 filename exist func Go Gin

//判断文件是否存在  存在返回 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
    }
}

标签:false,上传下载,filename,exist,func,Go,Gin
From: https://www.cnblogs.com/snakej/p/17528444.html

相关文章

  • 如何用 TDengine 预测 “未来”
    介绍TDengine™是一种开源的云原生时序数据库(TimeSeriesDatabase,TSDB),专为物联网(IoT)、连接汽车和工业物联网进行了优化。它能够高效地实时摄取、处理和监控一天内由数十亿个传感器和数据收集器产生的PB级别的数据。 许多用户将由物联网设备、汽车或IT基础设施生成的海量......
  • Django 用户权限 组权限
    创建权限fromusers.modelsimportUserfromdjango.contrib.auth.modelsimportUser,Permission,Groupfromdjango.contrib.contenttypes.modelsimportContentType#决定在那个app.models.pycontent_type=ContentType.objects.get_for_model(User)#添加权限perm=......
  • Go Gin JWT token 使用
    packagemainimport("github.com/dgrijalva/jwt-go""log""fmt""errors""time")//一些常量var(TokenExpirederror......
  • 直播软件搭建,生成二维码及添加logo
    直播软件搭建,生成二维码及添加logo  @Override  publicBitmapgenerateBitmap(Stringcontent,intwidth,intheight){    QRCodeWriterqrCodeWriter=newQRCodeWriter();    Map<EncodeHintType,String>hints=newHashMap<>();    h......
  • golang之http请求库go-resty
     github: https://github.com/go-resty/resty go-resty特性#go-resty 有很多特性:发起GET,POST,PUT,DELETE,HEAD,PATCH,OPTIONS,etc.请求简单的链式书写自动解析JSON和XML类型的文档上传文件重试功能客户端测试功能RestyclientCustom RootCertificates andC......
  • Django 数据库操作
    查询models.UserInfo.objects.all()models.UserInfo.objects.all().values('user')#只取user列models.UserInfo.objects.all().values_list('id','user')#取出id和user列,并生成一个列表models.UserInfo.objects.get(id=1)models.UserInfo.objects.get(u......
  • HuggingFace | 如何下载预训练模型
    本例我们在Linux上进行下载,下载的模型是bert-base-uncased。下载网址为:https://www.huggingface.co/bert-base-uncasedhuggingface的transformers框架,囊括了BERT、GPT、GPT2、ToBERTa、T5等众多模型,同时支持pytorch和tensorflow2,代码非常规范,使用也非常简单,但是模型使用的时候,......
  • 阿里云 MongoDB 创建库添加用户并授权
    先通过root进到admin库,右击test选择用户管理测试连接......
  • .net Core Api 注入 Microsoft.Extensions.Logging
    ILoggerAdapter.csusingSystem;usingSystem.Collections.Generic;usingSystem.Text;publicinterfaceILoggerAdapter<T>{////Summary://Formatsandwritesaninformationallogmessage.////P......
  • RunnerGo 新增对WebSocket、dubbo、TCP/IP三种协议的API测试
    大家好,RunnerGo作为一款一站式测试平台不断为用户提供更好的使用体验,最近得知RunnerGo新增对,WebSocket、Dubbo、TCP/IP,三种协议API的测试支持,本篇文章跟大家分享一下使用方法。WebSocket协议WebSocket是一种在单个TCP连接上进行全双工通信的API技术。相比于传统的HTTP......