Upload file
package main import ( "fmt" "/gin-gonic/gin" ) func main() { router := () // Limit the upload size for the form (default 32 MiB) // = 8 << 20 // 8 MiB ("/upload", func(c *) { // Single file file, _ := ("file") // Upload the file to the specified path (file, "./"+) //(, ("'%s' uploaded!", )) ().Add("Content-Disposition",("attachment;filename=%s",)) ("./"+) }) (":8080") }
Return the file to the front end
().Add("Content-Disposition",("attachment;filename=%s",)) ("./"+) //Return the file path and call the method automatically
middleware
Two ways to call middleware
Method 1: Continuous call in Use
v1 := ("v1").Use(middel())
v1 := ("v1").Use(middel(),middel2())
Method 2: Use chain call
v1 := ("v1").Use(middel()).Use(middel2())
Single middleware
package main import ( "fmt" "/gin-gonic/gin" ) func middel(){ return func(context *) { ("I'm in the first 1") () ("I'm after the method") } } func main() { r := () v1 := ("v1").Use(middel()) ("/test", func(c *) { ("I'm inside the method") (200, { "msg": true, }) }) (":8080") }
result
I'm in the first method 1
I'm inside the method
I'm after the method 1
Multiple middleware
package main import ( "fmt" "/gin-gonic/gin" ) func middel(){ return func(context *) { ("I'm in the first 1") () ("I'm after the method") } } func middel2(){ return func(context *) { ("I'm in the first 2") () ("I'm after 2") } } func main() { r := () v1 := ("v1").Use(middel(),middel2()) ("/test", func(c *) { ("I'm inside the method") (200, { "msg": true, }) }) (":8080") } //Like an onion
result
I'm in the first method 1
I'm in the first 2
I'm inside the method
I'm after the method 2
I'm after the method 1
The above is the detailed content of the example of the implementation of golang Gin upload files to the front-end and middleware. For more information about golang Gin upload files to the front-end middleware, please pay attention to my other related articles!