我正在使用文件上传器,并且需要请求有效负载中的详细信息以进行裁剪。
func Upload(w http.ResponseWriter, r *http.Request) { reader, err := r.MultipartReader() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } //copy each part to destination. for { part, err := reader.NextPart() if err == io.EOF { break } if part.FormName() == "avatar_data"{ // Read the content in "avatar_data" how? } if part.FileName() == "" { continue } dst, err := os.Create("/Users/macadmin/test/" + part.FileName()) defer dst.Close() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } if _, err := io.Copy(dst, part); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } } img, _ := imaging.Open("/Users/macadmin/test/cry3.jpg") if err != nil { panic(err) } rect := image.Rect(0, 0, 200, 500) // rect := image.Rectangle{20,20} dst := imaging.Crop(img, rect) err = imaging.Save(dst, "/Users/macadmin/test/cry4.jpg") if err != nil { panic(err) } //display success message. }
我没有10位代表来发布POST请求的图片,但是它有
内容处置:表单数据;name =“ avatar_data” {“ x”:528,“ y”:108,“高度”:864,“宽度”:864}
所以从avatar_data我所需要的x,y,height,和width。我知道我必须封送JSON,但是我不确定如何达到这一点?
x
y
height
width
multipart.Part实现io.Reader接口。
multipart.Part
io.Reader
if part.FormName() == "avatar_data" { j, err := ioutil.ReadAll(part) if err != nil { //do something } //j == []byte(`{"x":528,"y":108,"height":864,"width":864}`), do something with it. }