> For the complete documentation index, see [llms.txt](https://go.kaanksc.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://go.kaanksc.com/web-sunucu-server-ag-istemleri/gin-web-kuetuephanesi/gin-dosya-yuekleme.md).

# Gin Dosya Yükleme

Bu yazıda Gin kütüphanesinden `POST` isteği ile nasıl dosya yükleyeceğimizi göreceğiz.

Aşağıda projemizin dizin/dosya yapısı bulunuyor.

```
.
├── main.go
└── public
    └── index.html
```

İlk olarak `index.html` dosyamızı görelim.

```markup
<!doctype html>
<html lang="tr">

<head>
    <meta charset="utf-8">
    <title>Dosya Yükle</title>
</head>

<body>
    <h1>Yüklemek için 1 adet dosya seçin</h1>

    <form action="/yukle" method="post" enctype="multipart/form-data">
        İsim: <input type="text" name="isim"><br>
        E-posta: <input type="email" name="email"><br>
        Dosya: <input type="file" name="dosya"><br><br>
        <input type="submit" value="Yükle">
    </form>
</body>
```

Detaylıca değinmiyorum. `input` elementlerinin `name` özelliğinde ne isim verdiğimizi dikkat etmemiz yeterli. `form` elementinin action özelliğine `/yukle` adresini verdik.

`main.go` dosyamız ise aşağıdaki gibi olacaktır. Detaylarına yorum satırlarında değindim.

```go
package main

import (
	"fmt"
	"path/filepath"

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

func main() {

	router := gin.Default()
	//Yükleme yapacağımız dosyanın maksimum boyutunu
	//ayarlayalım.
	router.MaxMultipartMemory = 8 << 20 // 8 MiB

	//index.html dosyamızın bulunduğu klasörü static olarak
	//ekliyoruz ki index.html dosyamızı kullanabilelim.
	router.Static("/", "./website")

	//yukleme işlemini yapacak olduğumuz yönlendirmeyi
	//buradan ayarlıyoruz.
	router.POST("/yukle", func(c *gin.Context) {

		//form içerisindeki dosya hariç verilerimizi
		//atayalım
		isim := c.PostForm("isim")
		eposta := c.PostForm("email")

		// Dosyamızı ise özel fonksiyon ile atıyoruz
		dosya, hata := c.FormFile("dosya")

		//Hata kontrolü yapmayı unutmayalım.
		if hata != nil {
			c.String(400, fmt.Sprintf("Form Hatası: %s", hata.Error()))
			return
		}

		//yüklenecek olan dosyanın ismini alalım.
		dosyaismi := filepath.Base(dosya.Filename)

		//yüklenen dosyanın kaydedileceği konum
		// dizin + dosyaismi
		kayıtYeri := "./website/" + dosyaismi

		//dosyayı kaydedelim ve hata kontrolü yapalım.
		if hata := c.SaveUploadedFile(dosya, kayıtYeri); hata != nil {
			c.String(400, fmt.Sprintf("Dosya Yükleme Hatası: %s", hata.Error()))
			return
		}

		//Şuana kadar herhangi bir sıkıntı ile
		//karşılaşmadıysak, olumlu mesajımızı gösterelim.
		c.String(200, fmt.Sprintf("%s isimli dosya başarıyla yüklendi \nİsim: %s\nE-posta: %s", dosya.Filename, isim, eposta))
	})
	router.Run(":8080")
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://go.kaanksc.com/web-sunucu-server-ag-istemleri/gin-web-kuetuephanesi/gin-dosya-yuekleme.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
