HTTP & Large File Upload (using web.go)

3,144 views
Skip to first unread message

Tobias Kräntzer

unread,
Feb 7, 2011, 10:44:17 AM2/7/11
to golan...@googlegroups.com
Hi,

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

Andrew Gerrand

unread,
Feb 7, 2011, 10:53:21 AM2/7/11
to Tobias Kräntzer, golan...@googlegroups.com
If you use Go's http package, you could simply write a handler that
does something like this:

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

Tobias Kräntzer

unread,
Feb 7, 2011, 1:46:21 PM2/7/11
to golan...@googlegroups.com
Hi,

Thanks for the quick reply.
I guess that the body is not a file in memory but a "stream" of the data!?

Tobias.

Andrew Gerrand

unread,
Feb 8, 2011, 1:46:51 AM2/8/11
to Tobias Kräntzer, golan...@googlegroups.com
On 7 February 2011 19:46, Tobias Kräntzer <anagro...@me.com> wrote:
> Hi,
>
> Thanks for the quick reply.
> I guess that the body is not a file in memory but a "stream" of the data!?

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

Tobias Kräntzer

unread,
Feb 8, 2011, 12:15:34 PM2/8/11
to Andrew Gerrand, golan...@googlegroups.com

Sounds great.
Thanks.

Tobias.
>

Reply all
Reply to author
Forward
0 new messages