dpklt4291 2013-12-24 19:24
浏览 33
已采纳

转到-接受http发布多部分文件

I am trying to figure out how to accept/receive a HTTP Post in Go. I just want to be able to receive a file, grab its mime-type and save the file locally.

I've been searching all day but all I can find is how to send a file to some remote location but none of the examples I find cover receiving it.

Any help would be appreciated.

using Justinas' example and mixing with my existing experiment I've gotten this far but m.Post never seems to be called.

package main

import (
    "fmt"
    "io"
    "net/http"
    "os"
    "github.com/codegangsta/martini"
    "github.com/codegangsta/martini-contrib/render"
)

func main() {

    m := martini.Classic()

    m.Use(render.Renderer(render.Options{
        Directory: "templates", // Specify what path to load the templates from.
        Layout: "layout", // Specify a layout template. Layouts can call {{ yield }} to render the current template.
        Charset: "UTF-8", // Sets encoding for json and html content-types.
    }))


    m.Get("/", func(r render.Render) {
        fmt.Printf("%v
", "g./")
        r.HTML(200, "hello", "world")
    })

    m.Get("/:who", func(args martini.Params, r render.Render) {
        fmt.Printf("%v
", "g./:who")
        r.HTML(200, "hello", args["who"])
    })

    m.Post("/up", func(w http.ResponseWriter, r *http.Request) {
        fmt.Printf("%v
", "p./up")

        file, header, err := r.FormFile("file")
        defer file.Close()

        if err != nil {
            fmt.Fprintln(w, err)
            return
        }

        out, err := os.Create("/tmp/file")
        if err != nil {
            fmt.Fprintf(w, "Failed to open the file for writing")
            return
        }
        defer out.Close()
        _, err = io.Copy(out, file)
        if err != nil {
            fmt.Fprintln(w, err)
        }

        // the header contains useful info, like the original file name
        fmt.Fprintf(w, "File %s uploaded successfully.", header.Filename)
    })

    m.Run()
}
  • 写回答

1条回答 默认 最新

  • du3979 2013-12-24 20:57
    关注

    Go's net/http server handles this pretty, using mime/multipart package behind the scenes. You only need to call r.FormFile() on your *http.Request to get a multipart.File back.

    Here's a complete example. And the result of uploading a file with curl:

    justinas@ubuntu /tmp curl -i -F file=@/tmp/stuff.txt http://127.0.0.1:8080/
    HTTP/1.1 100 Continue
    
    HTTP/1.1 200 OK
    Date: Tue, 24 Dec 2013 20:56:07 GMT
    Content-Length: 37
    Content-Type: text/plain; charset=utf-8
    
    File stuff.txt uploaded successfully.%                                                                                              
    justinas@ubuntu /tmp cat file
    kittens!
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集