Go语言中的HTTP-POST文件多部分编程 [英] HTTP-POST file multipart programming in Go language
本文介绍了Go语言中的HTTP-POST文件多部分编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图使用Go软件包mime / multipart和http发送多部分表单,并且我需要一些帮助来解决它。
HTML将是:
< html>
< head>< title> Multipart Test< / title>< / head>
< body>
< form action =/ multipartenctype =multipart / form-datamethod =POST>
< label for =file>请选择一个文件< / label>
< input id =filetype =filename =file/>
< br>
< label for =input1>请写一些文字< / label>
< input id =input1type =textname =input1/>
< br>
< label for =input2>请再写一些文字< / label>
< input id =input2type =textname =input2/>
< br>
< input type =submitname =Submitvalue =Submit/>
< / body>
这:
var buffer bytes.Buffer
w:= multipart。 NewWriter(& buffer)
//写入字段和文件
w.CreateFormField(input1)
w.WriteField(input1,value1)
w。 CreateFormFile(file,filename.dat)
//我需要一个Reader来读取文件,但是如何?
//然后发送请求
resp,err:= http.Post(url,w.FormDataContentType(),& buffer)
感谢和问候
解决方案
可以在此示例代码中找到答案。
//上传文件到google代码
func上传(tarball字符串)(err os.Error){
//创建缓冲区
buf := new(bytes.Buffer)//警告IMO不要使用这个用于大文件,\
//创建一个tmpfile并从那里装配你的多部分(未测试)
w:= multipart.NewWriter buf)
//为字段标签创建一个表单字段编写器
label,err:= w.CreateFormField(label)
if err!= nil {
return err
}
//写入标签字段
label.Write([] byte(label here))
//为字段摘要创建一个表单字段编写器
汇总, 呃:= w.CreateFormField(summary)
if err!= nil {
return err
}
//写入摘要字段
summary.Write([]字节(摘要在这里))
//创建文件字段
fw,err:= w.CreateFormFile(upload,tarball)
if err!= nil {
返回err
}
fd,err:= os.Open(tarball)
if err!= nil {
return err
}
推迟fd。关闭()
//写文件字段从文件上传
_,err = io.Copy(fw,fd)
if err!= nil {
return err
}
//重要的是,如果你不关闭多部分写入器,你将不会有
//终止边界
w.Close()
req,err:= http .NewRequest(POST,repoUrl,buf)
if err!= nil {
return err
}
req.Header.Set(Content-Type,w。 FormDataContentType())
req.SetBasicAuth(email@email.com,password)
res,err:= client.Do(req)
if err!= nil {
return err
}
io.Copy(os.Stderr,res.Body)//将其替换为Status.Code check
return err
}
I'm trying to send a multipart form using Go packages mime/multipart and http, and I need some help to solve it.
The HTML would be:
<html>
<head><title>Multipart Test</title></head>
<body>
<form action="/multipart" enctype="multipart/form-data" method="POST">
<label for="file"> Please select a File </label>
<input id="file" type="file" name="file"/>
<br>
<label for="input1"> Please write some text </label>
<input id="input1" type="text" name="input1"/>
<br>
<label for="input2"> Please write some more text </label>
<input id="input2" type="text" name="input2"/>
<br>
<input type="submit" name="Submit" value="Submit"/>
</body>
And my Go approach is like this:
var buffer bytes.Buffer
w := multipart.NewWriter(&buffer)
// Write fields and files
w.CreateFormField("input1")
w.WriteField("input1","value1")
w.CreateFormFile("file","filename.dat")
// I need a Reader to here to read the file, but how ?
// then send the request
resp,err := http.Post(url,w.FormDataContentType(),&buffer)
Thanks and regards
解决方案
The answer can be found following this sample code
// Upload file to google code
func Upload(tarball string) (err os.Error) {
// Create buffer
buf := new(bytes.Buffer) // caveat IMO dont use this for large files, \
// create a tmpfile and assemble your multipart from there (not tested)
w := multipart.NewWriter(buf)
// Create a form field writer for field label
label, err := w.CreateFormField("label")
if err != nil {
return err
}
// Write label field
label.Write([]byte("label here"))
// Create a form field writer for field summary
summary, err := w.CreateFormField("summary")
if err != nil {
return err
}
// Write summary field
summary.Write([]byte("summary here"))
// Create file field
fw, err := w.CreateFormFile("upload", tarball)
if err != nil {
return err
}
fd, err := os.Open(tarball)
if err != nil {
return err
}
defer fd.Close()
// Write file field from file to upload
_, err = io.Copy(fw, fd)
if err != nil {
return err
}
// Important if you do not close the multipart writer you will not have a
// terminating boundry
w.Close()
req, err := http.NewRequest("POST", repoUrl, buf)
if err != nil {
return err
}
req.Header.Set("Content-Type", w.FormDataContentType())
req.SetBasicAuth("email@email.com", "password")
res, err := client.Do(req)
if err != nil {
return err
}
io.Copy(os.Stderr, res.Body) // Replace this with Status.Code check
return err
}
这篇关于Go语言中的HTTP-POST文件多部分编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文