Angularjs 如何使用Go从一个HTTP请求中解析文件和JSON数据?

Angularjs 如何使用Go从一个HTTP请求中解析文件和JSON数据?,angularjs,http,go,request,Angularjs,Http,Go,Request,我一直在想如何从Angularjs前端解析一个http请求表单中的PDF文档和JSON数据。 请求有效负载为 HTTP请求 内容配置:表单数据;name=“file”;filename=“家长手册.pdf” 内容类型:application/pdf 内容配置:表单数据;name=“doc” {“标题”:“试验”,“试验类别”:“试验类别”,“日期”:20142323} “file”是pdf,“doc”是我想要解析的json数据 我的处理程序可以很好地解析和保存文件,但无法从Json中获取任何信息

我一直在想如何从Angularjs前端解析一个http请求表单中的PDF文档和JSON数据。 请求有效负载为

HTTP请求

内容配置:表单数据;name=“file”;filename=“家长手册.pdf” 内容类型:application/pdf

内容配置:表单数据;name=“doc”

{“标题”:“试验”,“试验类别”:“试验类别”,“日期”:20142323}

“file”是pdf,“doc”是我想要解析的json数据

我的处理程序可以很好地解析和保存文件,但无法从Json中获取任何信息。有什么想法吗

func (s *Server) PostFileHandler(w http.ResponseWriter, r *http.Request) {
    const _24K = (1 << 20) * 24
    err := r.ParseMultipartForm(_24K)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    doc := Doc{}
    jsonDecoder := json.NewDecoder(r.Body)
    fmt.Println(r.Body)
    err = jsonDecoder.Decode(&doc)
    if err != nil {
        fmt.Println(err.Error())
    }
    fmt.Println(doc.Title, doc.Url, doc.Cat, doc.Date)
    doc.Id = len(docs) + 1
    err = s.db.Insert(&doc)
    checkErr(err, "Insert failed")

    // files := m.File["myFile"]
    for _, fheaders := range r.MultipartForm.File {
        for _, hdr := range fheaders {
            var infile multipart.File
            infile, err = hdr.Open()
            // defer infile.Close()
            if err != nil {
                http.Error(w, err.Error(), http.StatusInternalServerError)
                return
            }
            doc.Url = hdr.Filename
            fmt.Println(hdr.Filename)
            var outfile *os.File
            outfile, err = os.Create("./docs/" + hdr.Filename)
            // defer outfile.Close()
            if err != nil {
                http.Error(w, err.Error(), http.StatusInternalServerError)
                return
            }
            _, err = io.Copy(outfile, infile)
            if err != nil {
                http.Error(w, err.Error(), http.StatusInternalServerError)
                return
            }

        }
    }
    s.Ren.JSON(w, http.StatusOK, &doc)
    // http.Error(w, "hit file server", http.StatusOK)
}
func(s*Server)PostFileHandler(w http.ResponseWriter,r*http.Request){

const_24K=(1在您的示例中,您试图读取r.Body,就好像它是从请求的PDF部分中剥离出来的一样,但事实并非如此。您需要分别处理PDF和JSON这两个部分。 用这个

r、 MultipartReader()将返回对象,因此您可以使用函数迭代各个部分,并分别处理每个部分

因此,处理程序函数应该如下所示:

func (s *Server) PostFileHandler(w http.ResponseWriter, r *http.Request) {
    mr, err := r.MultipartReader()
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }

    doc := Doc{}
    for {
        part, err := mr.NextPart()

        // This is OK, no more parts
        if err == io.EOF {
            break
        }

        // Some error
        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
        }

        // PDF 'file' part
        if part.FormName() == "file" {
            doc.Url = part.FileName()
            fmt.Println("URL:", part.FileName())
            outfile, err := os.Create("./docs/" + part.FileName())
            if err != nil {
                http.Error(w, err.Error(), http.StatusInternalServerError)
                return
            }
            defer outfile.Close()

            _, err = io.Copy(outfile, part)
            if err != nil {
                http.Error(w, err.Error(), http.StatusInternalServerError)
                return
            }
        }

        // JSON 'doc' part
        if part.FormName() == "doc" {
            jsonDecoder := json.NewDecoder(part)
            err = jsonDecoder.Decode(&doc)
            if err != nil {
                http.Error(w, err.Error(), http.StatusInternalServerError)
                return
            }
            fmt.Println(doc.Title, doc.Url, doc.Cat, doc.Date)
        }
    }

    doc.Id = len(docs) + 1
    err = s.db.Insert(&doc)
    checkErr(err, "Insert failed")

    s.Ren.JSON(w, http.StatusOK, &doc)
}

感谢您花时间向我解释这一点。现在它变得更有意义了。我已经在这方面坚持了一个多星期了。为我省去了一个巨大的头痛,谢谢!!!错误为“http:multipart由ParseMultipartForm处理”