I have a little project now with which I would like to dive into the world of go. Unfortunately it contains a web server which has to handle uploads of large files (up to 1 GB). Is there an example how this could be done via a temporary file.
Greetings,
Tobias
func Upload(w http.ResponseWriter, r *http.Request) {
f, _ := os.Open("/tmp/upload", os.O_WRONLY|OS.O_CREATE|os.O_TRUNC, 0644)
io.Copy(f, r.Body)
r.Body.Close()
f.Close()
}
with (of course) error checking and writing the relevant http response
to the user.
Andrew
Thanks for the quick reply.
I guess that the body is not a file in memory but a "stream" of the data!?
Tobias.
Essentially, yes. The Body element of the Response is an io.Reader,
which will pass the data directly from the underlying TCP connection.
Only a small chunk of the stream should need to be in memory at any
one time. This is a really nice property of the io.Reader and
io.Writer interfaces.
Andrew
Sounds great.
Thanks.
Tobias.
>