Search code examples
httpgofile-uploadgo-iris

File Upload Failed using SIRIS


I want to upload a file to the server using SIRIS and Postman.

Go Program

package main

import (
    "github.com/go-siris/siris"
)

func main() {
    app := siris.New()
    app.Post("/", handleFileUpload)
    app.Run(siris.Addr(":8080"))
}

func handleFileUpload(ctx siris.Context) {
    ctx.Writef("Hello<br/>")
    file, info, err := ctx.FormFile("filee")
    if err != nil {
        ctx.StatusCode(iris.StatusInternalServerError)
        ctx.HTML("Error while uploading: <b>" + err.Error() + "</b>")
        return
    }
    defer file.Close()
    fn := info.Filename
    ctx.Writef("File Name: " + fn)
}

Postman

enter image description here

But Postman only can get the error message:

Hello
Error while uploading: request Content-Type isn't multipart/form-data

Why does this happen?


Solution

  • To correctly hanle file upload html form should have attribute

    enctype="multipart/form-data"
    

    enter image description here

    https://www.w3schools.com/php/php_file_upload.asp

    P.S. I do not recommend to use Iris.