如何通过Postman上传多个文件(已解决)

huangapple go评论126阅读模式
英文:

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 (
	&quot;net/http&quot;
	&quot;path/filepath&quot;

	&quot;github.com/gin-gonic/gin&quot;
)

func main() {
	router := gin.Default()

	router.Static(&quot;/&quot;, &quot;.public&quot;)
	router.POST(&quot;/&quot;, func(c *gin.Context) {

		form, _ := c.MultipartForm()

		files := form.File[&quot;files&quot;]

		for _, file := range files {
			filename := filepath.Base(file.Filename)
			if err := c.SaveUploadedFile(file, filename); err != nil {
				c.String(http.StatusBadRequest, &quot;erro ao carregar arquivo&quot;)
			}
		}
		c.String(http.StatusOK, &quot;upload realizado, %d files &quot;, len(files))
	})
	router.Run(&quot;:8080&quot;)
}

<!-- 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")
}

如何通过Postman上传多个文件(已解决)

英文:

This snippet seems to work

package main

import (
	&quot;github.com/gin-gonic/gin&quot;
	&quot;log&quot;
	&quot;mime/multipart&quot;
)

type BindFile struct {
	Name  string                  `form:&quot;name&quot; binding:&quot;required&quot;`
	Email string                  `form:&quot;email&quot; binding:&quot;required&quot;`
	Files []*multipart.FileHeader `form:&quot;file&quot; binding:&quot;required&quot;`
}

func main() {
	router := gin.Default()

	router.POST(&quot;/upload&quot;, func(c *gin.Context) {
		var bindFile BindFile

		c.ShouldBind(&amp;bindFile)
		for _, f := range bindFile.Files {
			log.Println(f.Filename)
		}
	})

	router.Run(&quot;:3000&quot;)
}

如何通过Postman上传多个文件(已解决)

huangapple
  • 本文由 发表于 2022年11月24日 20:44:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/74561014.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定