Handle Http Upload Zip file in Golang

I'm using golang net/http package to retrieve the uploaded zip file via postman. The attachment file link. It is not dangerous file. Feel free to check out.

Development env

  • local machine m1 macbook pro golang 1.17.2 - no issue
  • server docker image golang:1.17.5-stretch - got issue.

Code to capture the post form transSourceFile file.

func HandleFileReqTest(w http.ResponseWriter, req *http.Request, params map[string]string) err {

    if err := req.ParseMultipartForm(32 << 20); err != nil {
       return err
    }

    file, header, err := req.FormFile("transSourceFile")
    if err != nil {
       return err
    }
    defer file.Close()
    fmt.Println("header.Size:", header.Size)
    return nil
}

I tried below code also no use

func HandleFileReqTest(w http.ResponseWriter, req *http.Request, params map[string]string) err {
    if err := req.ParseForm(); err != nil {
        return err
    }
    req.ParseMultipartForm(32 << 20)
    file, header, err := req.FormFile("transSourceFile")
    if err != nil {
        return err
    }
    defer file.Close()
    fmt.Println("header.Size:", header.Size)
    return nil
}

Result: Local machine got the same file size as the origin file. Server with golang:1.17.5-stretch got the different file size compare to origin file.

As the result on this, i'm unable to unzip the file in the server. Anyone can help?


You need to copy form file to the actual file:

f, err := os.Create("some.zip")
defer f.Close()
n, err := io.Copy(f, file)