dqcqcqwq38860 2011-08-28 20:14
浏览 18

HTTP-POST文件多部分

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)

  • 写回答

1条回答 默认 最新

  • dongmen9517 2011-08-30 19:41
    关注

    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
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大