Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

[Question] How manage multiple upload file in forms? #1770

Closed
badstorm opened this issue Aug 20, 2019 · 5 comments · May be fixed by #2389
Closed

[Question] How manage multiple upload file in forms? #1770

badstorm opened this issue Aug 20, 2019 · 5 comments · May be fixed by #2389
Labels
s: triage Some tests need to be run to confirm the issue

Comments

@badstorm
Copy link

Description

Is there any way to manage multiple upload file in a form?

Steps to Reproduce the Problem

I try this way:

  1. Crate a new model and add this two fields:
Images      slices.String `json:"images" db:"images"`
ImagesForm  []binding.File  `json:"-" db:"-" form:"imageFiles"`
  1. In the Create method of the model I add:
func (r Resource) Create(c buffalo.Context) error {
    ... 

	var images []string
	for _, img := range myModel.ImagesForm {
		dir := filepath.Join(".", "uploads")
		images = append(images, filepath.Join(dir, img.Filename))
	}
	myModel.Images = images

    ...
}
  1. In the HTML form i add:
    <%= f.FileTag("ImagesForm", {multiple:"multiple"}) %>

Expected Behavior

I need that the slice with images paths must be saved in the Images field of the model.

Actual Behavior

The images var is always null.

Info

-> Go: Checking installation
✓ The `go` executable was found on your system at: /usr/local/go/bin/go

-> Go: Checking minimum version requirements
✓ Your version of Go, 1.12.7, meets the minimum requirements.

-> Go: Checking GOPATH
✓ You appear to be operating inside of your GOPATH.

-> Go: Checking Package Management
⚠ You do not appear to be using a package management system.

It is strongly suggested that you use one of the following package management systems:

* Go Modules (Recommended) - https://gobuffalo.io/en/docs/gomods
* Dep - https://github.com/golang/dep

For help setting up your Go environment please follow the instructions for you platform at:

https://www.gopherguides.com/courses/preparing-your-environment-for-go-development

-> Go: Checking PATH
✓ Your PATH contains /Users/marco.olimpi/Source/GO/bin.

-> Node: Checking installation
✓ The `node` executable was found on your system at: /usr/local/bin/node

-> Node: Checking minimum version requirements
✓ Your version of Node, v10.15.3, meets the minimum requirements.

-> NPM: Checking installation
✓ The `npm` executable was found on your system at: /usr/local/bin/npm

-> NPM: Checking minimum version requirements
✓ Your version of NPM, 6.4.1, meets the minimum requirements.

-> Yarn: Checking installation
✓ The `yarnpkg` executable was found on your system at: /usr/local/bin/yarnpkg

-> Yarn: Checking minimum version requirements
✓ Your version of Yarn, 1.17.3, meets the minimum requirements.

-> PostgreSQL: Checking installation
✘ The `postgres` executable could not be found on your system.
For help setting up your Postgres environment please follow the instructions for you platform at:

https://www.postgresql.org/download/

-> MySQL: Checking installation
✘ The `mysql` executable could not be found on your system.
For help setting up your MySQL environment please follow the instructions for you platform at:

https://www.mysql.com/downloads/

-> SQLite3: Checking installation
✓ The `sqlite3` executable was found on your system at: /usr/bin/sqlite3

-> SQLite3: Checking minimum version requirements
✓ Your version of SQLite3, 3.24.0, meets the minimum requirements.

-> Cockroach: Checking installation
✘ The `cockroach` executable could not be found on your system.
For help setting up your Cockroach environment please follow the instructions for you platform at:

https://www.cockroachlabs.com/docs/stable/

-> Buffalo: Checking installation
✓ The `buffalo` executable was found on your system at: /usr/local/bin/buffalo

-> Buffalo: Checking minimum version requirements
✓ Your version of Buffalo, v0.14.9, meets the minimum requirements.

-> Buffalo: Application Details
Pwd         /Users/marco.olimpi/Source/GO/src/duckpage.com/ido02
Root        /Users/marco.olimpi/Source/GO/src/duckpage.com/ido02
GoPath      /Users/marco.olimpi/Source/GO
PackagePkg  duckpage.com/ido02
ActionsPkg  duckpage.com/ido02/actions
ModelsPkg   duckpage.com/ido02/models
GriftsPkg   duckpage.com/ido02/grifts
WithModules false
Name        ido02
Bin         bin/ido02
VCS         git
WithPop     true
WithSQLite  false
WithDep     false
WithWebpack true
WithNodeJs  true
WithYarn    true
WithDocker  true
WithGrifts  true
AsWeb       true
AsAPI       false
InApp       true
PackageJSON {map[]}

-> Buffalo: config/buffalo-app.toml
name = "ido02"
bin = "bin/ido02"
vcs = "git"
with_pop = true
with_sqlite = false
with_dep = false
with_webpack = true
with_nodejs = true
with_yarn = true
with_docker = true
with_grifts = true
as_web = true
as_api = false

-> Buffalo: config/buffalo-plugins.toml
[[plugin]]
  binary = "buffalo-auth"
  go_get = "github.com/gobuffalo/buffalo-auth"

[[plugin]]
  binary = "buffalo-pop"
  go_get = "github.com/gobuffalo/buffalo-pop"
@paganotoni paganotoni added the s: triage Some tests need to be run to confirm the issue label Apr 8, 2020
@Norris1z
Copy link

Norris1z commented Apr 8, 2021

I have the same problem. But i was able to get it working by getting the image directly from the context using the File method

So something like context.File("imageFiles[0]") returns the first file or an error

@dmuriel
Copy link
Contributor

dmuriel commented Apr 16, 2021

Hello, I found myself in this exact situation, it would be great that buffalo supports multiples files. As a workaround, I had to implement a custom Bind, like:

type Images struct {
	Files []binding.File
}

func (v *Images) Bind(c interface{}) error {
	ctx, ok := c.(buffalo.Context)
	if !ok {
		return errors.Errorf("not a buffalo context")
	}

	if err := ctx.Request().ParseMultipartForm(5 * 1024 * 1024); err != nil {
		return errors.Wrap(err, "parsing multipart form error")
	}

	for _, f := range ctx.Request().MultipartForm.File["Images"] {
		file, err := f.Open()
		if err != nil {
			return errors.Wrap(err, "opening file error")
		}

		v.Files = append(v.Files, binding.File{
			File:       file,
			FileHeader: f,
		})
	}

	return nil
}

@dmuriel
Copy link
Contributor

dmuriel commented Apr 16, 2021

I was looking into "file_request_type_binder.go" and I did some tweaks in order to support "[]binding.File{}", it seems to work with a couple of tests that I did locally, but I'm not sure if this should solve the issue.

...
if f.Kind() == reflect.Slice {
    for _, fh := range req.MultipartForm.File[n] {
	    mf, err := fh.Open()
	    if err != nil {
		    return err
	    }
    
	    f.Set(reflect.Append(f, reflect.ValueOf(File{
		    File:       mf,
		    FileHeader: fh,
	    })))
    }
    
    return nil
}
...

@github-actions
Copy link

github-actions bot commented Aug 7, 2021

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions
Copy link

This issue was closed because it has been stalled for 5 days with no activity.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
s: triage Some tests need to be run to confirm the issue
Projects
None yet
4 participants