is there a example for upload file with net/http

1,619 views
Skip to first unread message

平民四月份

unread,
Apr 10, 2012, 2:59:08 AM4/10/12
to golan...@googlegroups.com

i want to post a form like this, how can i do it with go1 net/http, anybody who can give me a example ? thx

<form action="sql.aspx" method="post" enctype="multipart/form-data">  
        <input id="Text1" name="content" type="text" /><br />  
        <input id="Text2" name="uploadImg" type="text" /><br />  
        <input id="File1" type="file" name="image0" /><br />  
        <input id="Submit1" type="submit" value="submit" />  
</form>   
--
 Face the sea, for the spring flowers blossoming




zhai

unread,
Apr 10, 2012, 3:10:50 AM4/10/12
to 平民四月份, golan...@googlegroups.com


2012/4/10 平民四月份 <lew...@gmail.com>


i want to post a form like this, how can i do it with go1 net/http, anybody who can give me a example ? thx

<form action="sql.aspx" method="post" enctype="multipart/form-data">  
        <input id="Text1" name="content" type="text" /><br />  
        <input id="Text2" name="uploadImg" type="text" /><br />  
        <input id="File1" type="file" name="image0" /><br />  
        <input id="Submit1" type="submit" value="submit" />  
</form>   

func save(w http.ResponseWriter, r *http.Request) {
fn,header,err:= r.FormFile("image0")
        check(err)
defer fn.Close()

f, _ := os.Create("./upload/"+header.Filename)
defer f.Close()

       io.Copy(f,fn)


Kyle Lemons

unread,
Apr 10, 2012, 12:24:52 PM4/10/12
to 平民四月份, golan...@googlegroups.com
2012/4/9 平民四月份 <lew...@gmail.com>


i want to post a form like this, how can i do it with go1 net/http, anybody who can give me a example ? thx

<form action="sql.aspx" method="post" enctype="multipart/form-data">  
        <input id="Text1" name="content" type="text" /><br />  
        <input id="Text2" name="uploadImg" type="text" /><br />  
        <input id="File1" type="file" name="image0" /><br />  
        <input id="Submit1" type="submit" value="submit" />  
</form>   


Do you want to upload to that form or receive an upload in a go application? 

平民四月份

unread,
Apr 10, 2012, 3:52:43 PM4/10/12
to Kyle Lemons, golan...@googlegroups.com
i want to send a request but i do not know how to build a multipart
(multipart.CreateFormFile seems not fill the file's binary data to a
part )and link it to a request object

Kyle Lemons

unread,
Apr 10, 2012, 3:58:56 PM4/10/12
to 平民四月份, golan...@googlegroups.com
You have to create a file, write to it, and then close the writer, but before any of that you have to set the content type and boundary in the HTTP headers.


2012/4/10 平民四月份 <lew...@gmail.com>

平民四月份

unread,
Apr 10, 2012, 9:04:26 PM4/10/12
to Kyle Lemons, golan...@googlegroups.com
maybe some code will clear my intent:

buf := new(bytes.Buffer )
w := multipart.NewWriter(buf)

w.WriteField("content", "abcd" )
w.WriteField("content2", "1234" )
w.CreateFormFile("image0", "C:/example.jpg" )
w.Close()

after this. i print the buf:
fmt.Println( string(buf.Bytes()) )

--boudnary
Content-Disposition: form-data; name="content"

abcd
--boudnary
Content-Disposition: form-data; name="content2"

1234
--boundary
Content-Disposition: form-data; name="image0"; filename= "c:/example.jpg"
"Content-Type: image/jpeg"
//placeholder(add by me)
--boundary--

i want to see the placeholder is the binary data for "C:/example.jpg", but it is nothing, SO how to add the binary data to the multipart.


2012/4/11 Kyle Lemons <kev...@google.com>

Sanjay Menakuru

unread,
Apr 10, 2012, 9:36:20 PM4/10/12
to golan...@googlegroups.com, Kyle Lemons
When you call CreateFormFile, it returns an io.Writer. You have to write your data to that writer, use io.Copy from your in-memory bytes.Buffer, or io.Copy from your on-disk os.File.

Sanjay

Kyle Lemons

unread,
Apr 11, 2012, 1:49:42 AM4/11/12
to Sanjay Menakuru, golan...@googlegroups.com
On Tue, Apr 10, 2012 at 6:36 PM, Sanjay Menakuru <balas...@gmail.com> wrote:
When you call CreateFormFile, it returns an io.Writer. You have to write your data to that writer, use io.Copy from your in-memory bytes.Buffer, or io.Copy from your on-disk os.File.

You will still have to set the content-type and boundary in the HTTP headers before POSTing it will work. 

Reply all
Reply to author
Forward
0 new messages