Reputation: 229
I'm using a file uploader and need details from the request payload to crop it.
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.
}
I don't have 10 rep to post the image of the POST request, but it has
Content-Disposition: form-data; name="avatar_data" {"x":528,"y":108,"height":864,"width":864}
So from avatar_data I need the x
, y
, height
, and width
. I know I'll have to marshal the JSON but I'm not sure how to get to that point?
Upvotes: 2
Views: 2159
Reputation: 99215
multipart.Part
implements the io.Reader
interface.
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.
}
Upvotes: 4