Gin serve []byte as file


I have a golang backend with gin. Its supposed to work as a proxy for a ftp server that contains audiofiles. So my client should be able to call the endpoint /download/filepath the Golang application is then supposed to fetch the file from a ftp server and return it as a file(no stream).

My current solution is to read the file from the ftp as []byte and temporarily write it to disk with ioutil.TempFile() and return that file with ctx.File().

If possible I would like to skip the step where I’m creating a tempfile. Is that possible? Perhaps theres a way to proxy the call and directly connect the http call to the ftp call?

Note I don’t control the client application so I can’t change it.


Use Context.Data to write a []byte as the response.

You can also use Context.DataFromReader to copy directly from the FTP response to the HTTP response.

HTTP responses are a stream of bytes. There’s no way to get around that.

Answered By – thwd

Answer Checked By – Candace Johnson (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.