Gin is passed as a parameter for the back end to receive file picture uploaders

### Single file upload


Web # # # # at the front desk```html <! doctype html> <html lang="en">
<head>
    <meta charset="utf-8">
    <title>Single file upload</title>
</head>
<body>
<h1>Upload single file with fields</h1>

<form action="/upload" method="post" enctype="multipart/form-data">
    Name: <input type="text" name="name"><br>
    Email: <input type="email" name="email"><br>
    Files: <input type="file" name="file"><br><br>
    <input type="submit" value="Submit">
</form>
</body>
Copy the code

The form File input name must be File

<form action="/upload" method="post" enctype="multipart/form-data"> is the key attributeCopy the code

The key to uploading a file or image is encType =”multipart/form-data”, which is not bound to the argument described in the previous section

Gin backend

func main(a) {
	router := gin.Default()
	// Set a lower memory limit for multipart forms (default is 32 MiB)
	router.MaxMultipartMemory = 8 << 20 // 8 MiB
	router.Static("/"."./public")
	router.POST("/upload".func(c *gin.Context) {
		name := c.PostForm("name")
		email := c.PostForm("email")

		// Source
		file, err := c.FormFile("file")
		iferr ! =nil {
			c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
			return
		}

		filename := filepath.Base(file.Filename)
		iferr := c.SaveUploadedFile(file, filename); err ! =nil {
			c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))
			return
		}

		c.String(http.StatusOK, fmt.Sprintf("File %s uploaded successfully with fields name=%s and email=%s.", file.Filename, name, email))
	})
	router.Run(": 8080")}Copy the code

The back-end parsing

Name := c. ostForm("name") Email := C. ostForm("email") Obtains the HTML form parameter file, err := C. flocfile ("file") obtains the information about the form file.Copy the code

Multi-file upload

<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Multiple file upload</title>
</head>
<body>
<h1>Upload multiple files with fields</h1>

<form action="/upload" method="post" enctype="multipart/form-data">
    Name: <input type="text" name="name"><br>
    Email: <input type="email" name="email"><br>
    Files: <input type="file" name="files" multiple><br><br>
    <input type="submit" value="Submit">
</form>
</body>
</html>html

Copy the code

The key difference with a single file is

 type="file" name="files" multiple>
Copy the code
Multiple file uploads to gin backend
unc main() {
	router := gin.Default()
	// Set a lower memory limit for multipart forms (default is 32 MiB)
	router.MaxMultipartMemory = 8 << 20 // 8 MiB
	router.Static("/"."./public")
	router.POST("/upload".func(c *gin.Context) {
		name := c.PostForm("name")
		email := c.PostForm("email")

		// Multipart form
		form, err := c.MultipartForm()
		iferr ! =nil {
			c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
			return
		}
		files := form.File["files"]

		for _, file := range files {
			filename := filepath.Base(file.Filename)
			iferr := c.SaveUploadedFile(file, filename); err ! =nil {
				c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))
				return
			}
		}

		c.String(http.StatusOK, fmt.Sprintf("Uploaded successfully %d files with fields name=%s and email=%s.".len(files), name, email))
	})
	router.Run(": 8080")}Copy the code

Multiple file parameter binding

form, err := c.MultipartForm()
files := form.File["files"]
Copy the code

This gives you a list of uploaded files with a front-end name=”files”.

Note: form POST request