gobuffalo / buffalo

Rapid Web Development w/ Go
http://gobuffalo.io
MIT License
8.08k stars 576 forks source link

[Question] How pass file info in the response of a action? #1752

Closed badstorm closed 5 years ago

badstorm commented 5 years ago

Description

I need to return a file, that must before been elaborate, from an action. My solution works but there is no file size info ore a custum name.

Steps to Reproduce the Problem

My code has a function that generate a zip file and then an action to return the zip:

// in my action
func CustomZipWriter(w io.Writer, d render.Data) error {
...
}

func DownloadGet(c buffalo.Context) error {
    return c.Render(200, r.Func("application/zip", CustomZipWriter))
}

// in app.go
app.GET("/download/get", DownloadGet)

Expected Behavior

When i get the browser download popup it should show a custom name, like "02082019.zip" and the full size of the file so when the browser download it show the progress bar.

Actual Behavior

Using the code above, the file name is "get" and there is no size info so the browser show only the current downloaded bytes.

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.6, meets the minimum requirements. -> Go: Checking GOPATH ✘ You do not appear to be operating inside of your GOPATH. Things to check: * Capitalization - make sure that your capitalization of /home/marcoolimpi/go matches that of /home/marcoolimpi/go * `src` - make sure that you are working under /home/marcoolimpi/go/src GOPATH: /home/marcoolimpi/go PWD: /home/marcoolimpi/go Build Sources: /usr/local/go/src, /home/marcoolimpi/go/src 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 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 (/home/marcoolimpi/.vscode-server/bin/2213894ea0415ee8c85c5eea0d0ff81ecc191529/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files/dotnet/:/mnt/c/Program Files/Git/cmd:/mnt/c/Users/Marco Olimpi/AppData/Local/Programs/Python/Python37-32/Scripts/:/mnt/c/Users/Marco Olimpi/AppData/Local/Programs/Python/Python37-32/:/mnt/c/Users/Marco Olimpi/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/Marco Olimpi/AppData/Local/Programs/Microsoft VS Code/bin:/snap/bin:/usr/local/go/bin) does not contain /home/marcoolimpi/go/bin. Without /home/marcoolimpi/go/bin in your `PATH` any Go executables can not be run globally. 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 -> Node: Checking installation ✓ The `node` executable was found on your system at: /usr/bin/node -> Node: Checking minimum version requirements ✓ Your version of Node, v10.16.0, meets the minimum requirements. -> NPM: Checking installation ✓ The `npm` executable was found on your system at: /usr/bin/npm -> NPM: Checking minimum version requirements ✓ Your version of NPM, 6.9.0, meets the minimum requirements. -> Yarn: Checking installation ✓ The `yarnpkg` executable was found on your system at: /usr/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 could not be found on your system. For help setting up your SQLite3 environment please follow the instructions for you platform at: https://www.sqlite.org/download.html -> 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.7, meets the minimum requirements. -> Buffalo: Application Details Pwd /home/marcoolimpi/go Root /home/marcoolimpi/go GoPath /home/marcoolimpi/go PackagePkg ../go ActionsPkg ../go/actions ModelsPkg ../go/models GriftsPkg ../go/grifts WithModules false Name go Bin bin/go VCS WithPop false WithSQLite false WithDep false WithWebpack false WithNodeJs false WithYarn false WithDocker false WithGrifts false AsWeb true AsAPI false InApp false PackageJSON {map[]} ```
tatang26 commented 5 years ago

you can use:

func DownloadGet(c buffalo.Context) error {
    ...

    yourFileData := []byte{...}

    c.Response().Header().Set("Content-Disposition", "attachment;filename=<YOUR_FILE_NAME>")
    _, err = c.Response().Write(yourFileData)

    return err
}
badstorm commented 5 years ago

Thanks, that was just what I was looking for.

markbates commented 5 years ago

https://godoc.org/github.com/gobuffalo/buffalo/render#Engine.Download