ایک فائل اپ لوڈ
const maxSize = 8 * iris.MB
func main() {
app := iris.Default()
app.Post("/upload", func(ctx iris.Context) {
// درخواست کی بادی کی مقدار کی حد مقرر کریں، جو فائل اپ لوڈ کا حجم محدود کرتا ہے (پہلے سے مقرر 32 میگابائٹ)
ctx.SetMaxRequestBodySize(maxSize)
// یا
// app.Use(iris.LimitRequestBodySize(maxSize))
// یا
// iris.WithPostMaxMemory(maxSize)
// فائل پڑھیں
file, fileHeader, err:= ctx.FormFile("file")
if err != nil {
ctx.StopWithError(iris.StatusBadRequest, err)
return
}
// اپ لوڈ کی گئی فائل کو مخصوص ڈائرکٹری میں محفوظ کریں
dest := filepath.Join("./uploads", fileHeader.Filename)
ctx.SaveFormFile(fileHeader, dest)
ctx.Writef("فائل: %s اپ لوڈ ہوگئی ہے!", fileHeader.Filename)
})
app.Listen(":8080")
}
فائل ٹیسٹ اپ لوڈ کریں
curl -X POST http://localhost:8080/upload \
-F "file=@/Users/kataras/test.zip" \
-H "Content-Type: multipart/form-data"
متعدد فائل اپ لوڈ
func main() {
app := iris.Default()
app.Post("/upload", func(ctx iris.Context) {
// متعدد فائلوں کو پڑھیں
files, n, err := ctx.UploadFormFiles("./uploads")
if err != nil {
ctx.StopWithStatus(iris.StatusInternalServerError)
return
}
ctx.Writef("%d فائلیں کل %d حجم کے اپ لوڈ ہوگئی ہیں!", len(files), n))
})
app.Listen(":8080", iris.WithPostMaxMemory(8 * iris.MB))
}
متعدد فائلوں کا ٹیسٹ اپ لوڈ کریں
curl -X POST http://localhost:8080/upload \
-F "upload[]=@/Users/kataras/test1.zip" \
-F "upload[]=@/Users/kataras/test2.zip" \
-H "Content-Type: multipart/form-data"