我一直试图找出如何从Angularjs前端的一个HTTP请求表单中解析PDF文档和JSON数据。请求有效负载为
HTTP请求
内容处置:表单数据;name =“文件”; filename =“ Parent Handbook.pdf”内容类型:application / pdf
内容处置:表单数据;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)
}