Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to upload array of files with params #774

Closed
selvam347 opened this issue Dec 23, 2016 · 5 comments
Closed

How to upload array of files with params #774

selvam347 opened this issue Dec 23, 2016 · 5 comments

Comments

@selvam347
Copy link

I can upload single file using https://github.com/gin-gonic/gin/issues/548.

package main
import "github.com/gin-gonic/gin"
import "log"
import "net/http"
import "io"
import "os"

func main() {
	router := gin.Default()
	router.POST("/upload", func(c *gin.Context) {
		file, handler, err := c.Request.FormFile("upload")
		filename := handler.Filename
		log.Println("Received file:", handler.Filename)
		out, err := os.Create("./tmp/" + filename)
		if err != nil {
			log.Fatal(err)
		}
		defer out.Close()
		_, err = io.Copy(out, file)
		if err != nil {
			log.Fatal(err)
		}
		c.String(http.StatusOK, "Uploaded...")
	})
	router.Run(":8080")
}

But how to upload array of files like the following,
curl -X POST http://localhost:8080/upload -F "images[]=@/home/amp/hack/property1.jpg" -F "images[]=@/home/ack/property2.jpeg" -H "Content-Type: multipart/form-data"

And give me suggestion for the same above code, how to get some parameters with documents. Finally request will be like,

{
  "client": {
    "name": "",
    "Industry": "",
    "profile_image": "PROFILE-IMAGE-FILE",
    "supporting_docs": {
      "docs": [
        "DOCUMENT1-FILE",
        "DOCUMENT2-FILE"
      ],
      "comments": "Comment test"
    }
  }
} 
@tboerger
Copy link
Contributor

@andreynering does this also work with your form array value patch?

@andreynering
Copy link
Contributor

Haven't tried by I don't think so.

c.Request.FormFile is Go's method, not Gin's.

@appleboy
Copy link
Member

appleboy commented Dec 24, 2016

@tboerger @selvam347 @andreynering

I create new PR to support upload single or multiple files. #775

upload single file:

package main

import (
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

func main() {
	router := gin.Default()
	router.POST("/upload", func(c *gin.Context) {
		// single file
		file, _ := c.FormFile("file")
		log.Println(file.Filename)

		c.String(http.StatusOK, "Uploaded...")
	})
	router.Run(":8080")
}

curl command:

curl -X POST http://localhost:8080/upload -F "file=@/Users/mtk10671/z.sh" -H "Content-Type: multipart/form-data"

upload multiple file:

package main

import (
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

func main() {
	router := gin.Default()
	router.POST("/upload", func(c *gin.Context) {
		// Multipart form
		form, _ := c.MultipartForm()
		files := form.File["upload[]"]

		for _, file := range files {
			log.Println(file.Filename)
		}
		c.String(http.StatusOK, "Uploaded...")
	})
	router.Run(":8080")
}

curl command:

curl -X POST http://localhost:8080/upload -F "upload[]=@/Users/mtk10671/z.sh" -F "upload[]=@/Users/mtk10671/z.sh" -H "Content-Type: multipart/form-data"

@appleboy
Copy link
Member

Closed by #775

@roushanj
Copy link

roushanj commented Feb 22, 2018

How to upload In S3 Bucket ?
verification

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants