# Upload File

Upload file di gin dapat menggunakan method FormFile() untuk mendapatkan multipart file dari request dan SaveUploadFile() untuk menyimpan file di direktori yang diinginkan. Contohnya seperti _code_ di bawah ini yang digunakan untuk menyimpan gambar.

```go
package main

import (
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	r.GET("/", Upload)
	r.Run(":5000")
}

func Upload(ctx *gin.Context) {
	file, _ := ctx.FormFile("picture")
	err := ctx.SaveUploadedFile(file, fmt.Sprintf("./files/%s", file.Filename))
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"status":  http.StatusOK,
			"message": err,
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"status":  http.StatusOK,
		"message": fmt.Sprintf("'%s' has been uploaded: ", file.Filename),
	})
}

```

<figure><img src="https://1578455751-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F09pucuEBc5BTaaur3uoo%2Fuploads%2FNNHbY7cdPGa7L2pioPNT%2F1.png?alt=media&#x26;token=e4a0b9da-ac7d-4604-8d2c-0a4951d9123e" alt=""><figcaption></figcaption></figure>

## Upload lebih dari 1 file (.png dan .txt)

```go
package main

import (
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	r.GET("/", Upload)
	r.Run(":5000")
}

func Upload(ctx *gin.Context) {
	file, _ := ctx.FormFile("picture")
	err := ctx.SaveUploadedFile(file, fmt.Sprintf("./files/%s", file.Filename))
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"status":  http.StatusOK,
			"message": err,
		})
		return
	}

	file2, _ := ctx.FormFile("txt")
	err = ctx.SaveUploadedFile(file2, fmt.Sprintf("./files/%s", file2.Filename))
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"status":  http.StatusOK,
			"message": err,
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"status":  http.StatusOK,
		"message": fmt.Sprintf("'%s' and '%s' has been uploaded: ", file.Filename, file2.Filename),
	})
}

```

<figure><img src="https://1578455751-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F09pucuEBc5BTaaur3uoo%2Fuploads%2FEnuqfJm2O2rPePD2xl7m%2F1.png?alt=media&#x26;token=ad910171-190d-45a1-aec8-7b3882011fac" alt=""><figcaption></figcaption></figure>
