第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何使用 Go 從一個(gè) HTTP 請(qǐng)求中解析文件和 JSON 數(shù)據(jù)?

如何使用 Go 從一個(gè) HTTP 請(qǐng)求中解析文件和 JSON 數(shù)據(jù)?

Go
森林海 2021-09-21 22:45:33
我一直在試圖弄清楚如何從 Angularjs 前端的一個(gè) http 請(qǐng)求表單中解析 PDF 文檔和 JSON 數(shù)據(jù)。請(qǐng)求有效載荷是HTTP 請(qǐng)求內(nèi)容配置:表單數(shù)據(jù);名稱=“文件”;filename="Parent Handbook.pdf" 內(nèi)容類型:application/pdf內(nèi)容配置:表單數(shù)據(jù);名稱=“文檔”{"title":"test","cat":"test cat","date":20142323}“file”是pdf,“doc”是我要解析的json數(shù)據(jù)。我的處理程序可以很好地解析和保存文件,但無(wú)法從 Json 中獲取任何內(nèi)容。有任何想法嗎?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)}
查看完整描述

1 回答

?
猛跑小豬

TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個(gè)贊

在您的示例中,您試圖讀取 r.Body 就好像它是從請(qǐng)求的 PDF 部分中剝離出來的一樣,但事實(shí)并非如此。您需要分別處理 PDF 和 JSON 這兩個(gè)部分。使用http.(*Request).MultipartReader()。


r.MultipartReader() 將返回mime/multipart.Reader對(duì)象,因此您可以使用r.NextPart()函數(shù)迭代各個(gè)部分并分別處理每個(gè)部分。


所以你的處理函數(shù)應(yīng)該是這樣的:


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)

}


查看完整回答
反對(duì) 回復(fù) 2021-09-21
  • 1 回答
  • 0 關(guān)注
  • 383 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)