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":"参数传递有误!"}) } } }