英文:
How to upload many files via postman (RESOLVIDO)
问题
我正在使用Go开发一个上传API。
我可以使用Postman一次上传一个文件,使用body >> form-data。
但是我无法上传多个文件,有什么解决办法吗?
代码:
package main
import (
"fmt"
"mime/multipart"
"net/http"
"path/filepath"
"github.com/gin-gonic/gin"
)
type BindFile struct {
Name string `form:"name" binding:"required"`
Email string `form:"email" binding:"required"`
File *multipart.FileHeader `form:"file" binding:"required"`
}
func main() {
router := gin.Default()
router.POST("/upload", func(c *gin.Context) {
var bindFile BindFile
c.ShouldBind(&bindFile)
file := bindFile.File
dst := filepath.Base(file.Filename)
c.SaveUploadedFile(file, dst)
c.String(http.StatusOK, fmt.Sprintf("文件 %s 上传成功", file.Filename))
})
router.Run(":8080")
}
我尝试在我的form-data中添加一个文件数组:file[],但它没有起作用。
英文:
I am developing an upload api using Go.
I can upload one file at a time via postman with body >> form-data
but I can't upload more than 1 file, any solution?
code:
package main
import (
"fmt"
"mime/multipart"
"net/http"
"path/filepath"
"github.com/gin-gonic/gin"
)
type BindFile struct {
Name string `form:"name" binding:"required"`
Email string `form:"email" binding:"required"`
File *multipart.FileHeader `form:"file" binding:"required"`
}
func main() {
router := gin.Default()
router.POST("/upload", func(c *gin.Context) {
var bindFile BindFile
c.ShouldBind(&bindFile)
file := bindFile.File
dst := filepath.Base(file.Filename)
c.SaveUploadedFile(file, dst)
c.String(http.StatusOK, fmt.Sprintf("arquivo %s upado com sucesso", file.Filename))
})
router.Run(":8080")
}
I tried to put a file array in my form-data: file[] but it didn't work
答案1
得分: 2
我成功解决了这个问题,解决方法如下:
package main
import (
"net/http"
"path/filepath"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
router.Static("/", ".public")
router.POST("/", func(c *gin.Context) {
form, _ := c.MultipartForm()
files := form.File["files"]
for _, file := range files {
filename := filepath.Base(file.Filename)
if err := c.SaveUploadedFile(file, filename); err != nil {
c.String(http.StatusBadRequest, "erro ao carregar arquivo")
}
}
c.String(http.StatusOK, "upload realizado, %d files ", len(files))
})
router.Run(":8080")
}
来源:https://github.com/gin-gonic/examples/blob/master/upload-file/multiple/main.go
英文:
I managed to solve it as follows
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
package main
import (
"net/http"
"path/filepath"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
router.Static("/", ".public")
router.POST("/", func(c *gin.Context) {
form, _ := c.MultipartForm()
files := form.File["files"]
for _, file := range files {
filename := filepath.Base(file.Filename)
if err := c.SaveUploadedFile(file, filename); err != nil {
c.String(http.StatusBadRequest, "erro ao carregar arquivo")
}
}
c.String(http.StatusOK, "upload realizado, %d files ", len(files))
})
router.Run(":8080")
}
<!-- end snippet -->
source:
https://github.com/gin-gonic/examples/blob/master/upload-file/multiple/main.go
答案2
得分: 0
这段代码似乎可以工作。
package main
import (
"github.com/gin-gonic/gin"
"log"
"mime/multipart"
)
type BindFile struct {
Name string `form:"name" binding:"required"`
Email string `form:"email" binding:"required"`
Files []*multipart.FileHeader `form:"file" binding:"required"`
}
func main() {
router := gin.Default()
router.POST("/upload", func(c *gin.Context) {
var bindFile BindFile
c.ShouldBind(&bindFile)
for _, f := range bindFile.Files {
log.Println(f.Filename)
}
})
router.Run(":3000")
}
英文:
This snippet seems to work
package main
import (
"github.com/gin-gonic/gin"
"log"
"mime/multipart"
)
type BindFile struct {
Name string `form:"name" binding:"required"`
Email string `form:"email" binding:"required"`
Files []*multipart.FileHeader `form:"file" binding:"required"`
}
func main() {
router := gin.Default()
router.POST("/upload", func(c *gin.Context) {
var bindFile BindFile
c.ShouldBind(&bindFile)
for _, f := range bindFile.Files {
log.Println(f.Filename)
}
})
router.Run(":3000")
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论