HTTP-POST 文件多部分

发布于 2024-12-01 20:08:38 字数 1145 浏览 2 评论 0原文

我正在尝试使用 Go 包 mime/multipart 和 http 发送多部分表单,我需要一些帮助来解决它。

HTML 将是:


<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>

我的 Go 方法是这样的:


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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

作业与我同在 2024-12-08 20:08:38

答案可以在此示例代码中找到

// 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 protected]", "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
}

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 protected]", "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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文