Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix upload array files. #53

Merged
merged 2 commits into from
Jul 1, 2016
Merged

Fix upload array files. #53

merged 2 commits into from
Jul 1, 2016

Conversation

ghostiam
Copy link
Contributor

@ghostiam ghostiam commented Jul 1, 2016

If the fields have the same name and at least one of them is not filled, the files are not uploaded.

example:

package main

import (
    "github.com/go-martini/martini"
    "github.com/martini-contrib/binding"
    "mime/multipart"
    "net/http"
    "encoding/json"
)

type UploadForm struct {
    UploadFiles []*multipart.FileHeader `form:"uploadFiles"`
}

func main() {
    m := martini.Classic()

    m.Get("", func(w http.ResponseWriter, r *http.Request) {
        html := `
<form method="post" enctype="multipart/form-data">
    <input type="file" name="uploadFiles"><br>
        <input type="file" name="uploadFiles"><br>
        <input type="file" name="uploadFiles"><br>
        <input type="file" name="uploadFiles"><br>
        <button type="submit" name="save">Save</button>
</form>
        `

        w.Header().Set("Content-Type", "text/html; charset=UTF-8")
        w.WriteHeader(200)
        w.Write([]byte(html))
    })

    m.Post("", binding.Bind(UploadForm{}), func(r *http.Request, uf UploadForm) string {
        formJson, _ := json.MarshalIndent(r.MultipartForm, "", " ")
        filesJson, _ := json.MarshalIndent(uf, "", " ")

        return string(formJson) + "\n" + string(filesJson)
    })

    m.RunOnAddr(":3001")
}

If we fill in only one field of 4 we get the 3 null and 0 files in the variable "uf", although "r.MultipartForm" all files are available.
While we do not fill in all fields 4 "input [type = file]", the files will not be the transmitted through Binding.

The reason for this, filling the data to null when the data bindings of "r.MultipartForm.Value"

Before this fix, we get this (upload file 1 of 4):

// r.MultipartForm
{
 "Value": {
  "save": [
   ""
  ],
  "uploadFiles": [
   "",
   "",
   ""
  ]
 },
 "File": {
  "uploadFiles": [
   {
    "Filename": "main.go",
    "Header": {
     "Content-Disposition": [
      "form-data; name=\"uploadFiles\"; filename=\"main.go\""
     ],
     "Content-Type": [
      "application/octet-stream"
     ]
    }
   }
  ]
 }
}
// UploadForm
{
 "UploadFiles": [ // 3 null
  null,
  null,
  null
 ]
}

After this fix (upload file 1 of 4):

// r.MultipartForm
{
 "Value": {
  "save": [
   ""
  ],
  "uploadFiles": [
   "",
   "",
   ""
  ]
 },
 "File": {
  "uploadFiles": [
   {
    "Filename": "main.go",
    "Header": {
     "Content-Disposition": [
      "form-data; name=\"uploadFiles\"; filename=\"main.go\""
     ],
     "Content-Type": [
      "application/octet-stream"
     ]
    }
   }
  ]
 }
}
// UploadForm
{
 "UploadFiles": [ // Not null, yay!
  {
   "Filename": "main.go",
   "Header": {
    "Content-Disposition": [
     "form-data; name=\"uploadFiles\"; filename=\"main.go\""
    ],
    "Content-Type": [
     "application/octet-stream"
    ]
   }
  }
 ]
}

@ghostiam ghostiam mentioned this pull request Jul 1, 2016
@mholt mholt merged commit 05d3e15 into martini-contrib:master Jul 1, 2016
@mholt
Copy link
Contributor

mholt commented Jul 1, 2016

Thanks!

@ghostiam ghostiam deleted the FixUploadFiles branch July 1, 2016 18:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants