我正在使用文件上传器并需要请求有效负载中的详细信息来裁剪它。
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请求的图像,但它有
Content-Disposition: form-data; name="avatar_data" {"x":528,"y":108,"height":864,"width":864}
因此,从avatar_data我需要x
,y
,height
和width
。我知道我必须整理JSON,但我不确定如何达到这一点?
答案 0 :(得分:4)
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.
}