问题描述

我一直试图找出如何从Angularjs前端的一个http请求表单中解析PDF文档和JSON数据. 请求有效负载为

HTTP请求

Content-Disposition:表单数据; name =文件"; filename ="Parent Handbook.pdf" 内容类型:application/pdf

Content-Disposition:表单数据; name ="doc"

{"title":"test","cat":"test cat","date":20142323}

文件"是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)
}

在您的示例中,您尝试读取r.Body,就好像它已从请求的PDF部分中剥离出来一样,但事实并非如此.您需要分别处理PDF和JSON这两个部分. 为此,请使用 http.(* Request).MultipartReader().. >

r.MultipartReader()将返回 mime/multipart.Reader 对象,因此您可以使用 r.NextPart()函数对零件进行迭代并处理每个零件分开分开.

因此您的处理程序函数应如下所示:

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

I am stuck trying to figure out how to parse both a PDF document and JSON data out of one http request form from an Angularjs front end. The request payload is

HTTP Request

Content-Disposition: form-data; name="file"; filename="Parent Handbook.pdf" Content-Type: application/pdf

Content-Disposition: form-data; name="doc"

{"title":"test","cat":"test cat","date":20142323}

The "file" is the pdf and "doc" is the json data I want to parse.

My handler can parse and save the file just fine but fails to get anything out of the Json. Any ideas?

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

In your example you're trying to read r.Body as if it was stripped out of PDF part of the request, but it isn't. You need to process separately both parts, PDF and JSON. Use http.(*Request).MultipartReader() for that.

r.MultipartReader() will return you mime/multipart.Reader object, so you can iterate over parts with r.NextPart() function and process each part separately.

So your handler function should be like this:

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

这篇关于如何使用Go从一个HTTP请求中解析文件和JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!