3

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?

9
  • Nothing in the question is specific to docker or that it is a ZIP file - it is only about uploading a file and not receiving what was uploaded at the server. I've removed these misleading tags and also changed the title. Commented Jan 13, 2022 at 6:22
  • "Server with golang:1.17.5-stretch got the different file size compare to origin file." - what exactly is different size: more or less than the original? If less: do the transferred bytes match, i.e. it is only missing the end? If more: do the original bytes match, i.e. is there only junk at the end? Or what exactly is different here? Also maybe the upload itself is broken and the brokeness is only differently interpreted in different golang versions? Please provide exactly how the upload looks like (like with a packet capture). Commented Jan 13, 2022 at 6:24
  • @SteffenUllrich Thank you removing the misleading tags if i did. it is added because it might be development issue. Commented Jan 13, 2022 at 7:35
  • @SteffenUllrich the file size in server is more than origin file size. btw. so far the main problem is not on unable to unzip the file. The main issue is on the zip file header.Size value in from req.FormFile("transSourceFile") shows more bytes compare to origin. Origin have 171 bytes, header.Size value is 205. Can you suggest me on how to compare this 2 files? Commented Jan 13, 2022 at 7:43
  • @SteffenUllrich I had undo back the title with zip. So far it is only happen in server handle zip file. i had tried server handle non-zip file. non-zip file is working. i face no issue on local zip file. Commented Jan 13, 2022 at 8:09

2 Answers 2

2

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)
Sign up to request clarification or add additional context in comments.

2 Comments

it depends on uploaded size. file can be in memory: cs.opensource.google/go/go/+/refs/tags/go1.17.6:src/mime/…
Thanks for the idea. i did copied form file to actual file but this is process after i get the file value. So far what I'm facing is the header.Size value from req.FormFile("transSourceFile") are different. Current situation is In my local machine, the header.Size value i got is 171 which is same as origin file. while in server, the header.Size value i got is 205 which is different with origin file. This is happened in zip file for so far as i know. Others file type no idea yet.
0

Data isn't being flushed to the file completely. You should close the file first to ensure that the data is fully flushed.

  // create a local file filename
    dst, err := os.Create("filename.zip")

      // save it
     fl, err = io.Copy(dst, src)

     // Close the file
     dst.Close()

     stat, _ := dst.Stat()

 //Now check the size stat.Size() or header.Size after flushing the file.

1 Comment

It is not related on creating the new file yet. The problem is i got the wrong data info from req.FormFile("transSourceFile"). If got the wrong data, it will save with wrong data also. This issue only happenned on server zip file. i face no issue on local zip file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.