• go实现多个文件上传


    // UploadCodeHandler 上传代码
    func UploadCodeHandler(c *gin.Context){
        userId := c.PostForm("userId")
        projectId := c.PostForm("projectId")
        fmt.Println(userId)
        //Source
        form,err:=c.MultipartForm()
        //错误处理
        if err != nil {
            c.JSON(http.StatusBadRequest, gin.H{
                "status": -1,
                "error": err,
            })
        }else {
            //var fileUploadStatus []common.FileUploadStatus
            files :=form.File["file"]
            var fileUploadStatus =make([]common.FileUploadStatus,len(files))
            fmt.Println(len(files))
            if userId !="" && len(files)!=0 {
                var uploadT = "code"
                var filePaths =make([]string,len(files))
                fmt.Println(filePaths)
                for key,f:=range files{
                    fmt.Println(key)
                    fmt.Println(f.Filename)
                    fmt.Println("**************************")
                    fmt.Println(f.Header.Get("Content-Type"))
                    //c.SaveUploadedFile(f,f.Filename)
                    //fmt.Println(f.Header.Values("Content-Type")[0])
                    c.SaveUploadedFile(f,"./dataset/"+uploadT+"/"+f.Filename)
                    filePaths[key]="./dataset/"+uploadT+"/"+f.Filename
                    contentType :=f.Header.Get("Content-Type")
                    //contentType:="application/pdf"
                    fileIsExist :=db.CodeFileIsExist(f.Filename)
                    if fileIsExist==0{
                        userIdInt, _ := strconv.Atoi(userId)
                        db.AddCodeFile(userIdInt,f.Filename,contentType,projectId)
                        fileUploadStatus[key].Filename=f.Filename
                        fileUploadStatus[key].Status="上传成功!"
                        //c.JSON(http.StatusOK, gin.H{
                        //    "status": 1,
                        //    "message":filePaths,
                        //})
                    }else {
                        fileUploadStatus[key].Filename=f.Filename
                        fileUploadStatus[key].Status="文件已经存在!"
                        //c.JSON(http.StatusOK, gin.H{
                        //    "status": 2,
                        //    "message":"文件已经存在!",
                        //})
                    }
                }
                c.JSON(http.StatusOK, gin.H{"status": 1, "message":fileUploadStatus})
            }else {
                c.JSON(http.StatusOK, gin.H{"status": -1, "message":"参数传递有误!"})
            }
        }
    }
  • 相关阅读:
    ubuntu apt 命令相关
    linux 常用命令
    sqli-labs 通关详解(sql注入知识点整理)
    运维手册2---查看机子的系统信息
    运维手册1---ssh命令:使用密钥文件进行登陆
    Java中的==和equals,常量池
    Java Persistence
    VSCode常用插件汇总
    VSCode常用插件之Auto Rename Tag使用
    ESLint 使用指南
  • 原文地址:https://www.cnblogs.com/lxz123/p/15843792.html
Copyright © 2020-2023  润新知