Uploading Files Post Request – GinGonic golang framework 14

Uploading Files Post Request – GinGonic golang framework 14

Source Code

<div>
<div>// File Upload in GinGonic :)</div>
<div>package main</div>
<div>import (</div>
<div>    "path/filepath"</div>
<div>    "github.com/gin-gonic/gin"</div>
<div>)</div>
<div>func main() {</div>
<div>    r := gin.Default()</div>
<div>    r.Static("/", "./public") // our static files dir/folder</div>
<div>    // post request == uploading files via post req</div>
<div>    r.POST("/upload", func(c *gin.Context) {</div>
<div>        file, _ := c.FormFile("file") // file is the name of html input name="file"</div>
<div>        filePath := filepath.Base(file.Filename) // we will get the base file name</div>
<div>        c.SaveUploadedFile(file, filePath)       // first arg is file itself</div>
<div>        // 2nd arg is the filepath+file name</div>
<div>        c.String(200, "file Uploaded...")</div>
<div>        // you can do someting like this also</div>
<div>        //c.Redirect(200, "/")</div>
<div>        // Lets create html file first and public folder also</div>
<div>    })</div>
<div>    r.Run()</div>
<div>}</div>
</div>

TAGS#

gin gonic tutorial
gin-gonic / examples
gin-gonic documentation
gin framework tutorial
gin web framework
gin router
golang gin context
gin router group

###Buy Me a Coffee https://www.buymeacoffee.com/khan1

Tony BB
 

TonyBB is a Coach , marketer, hypnotist and a founder of RSKVF Production who specializes in providing simple, affordable, and easy to use solutions for Life.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: