英文:
Read request payload in Go?
问题
我正在使用一个文件上传器,并需要从请求负载中获取详细信息来进行裁剪。
func Upload(w http.ResponseWriter, r *http.Request) {
reader, err := r.MultipartReader()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// 将每个部分复制到目标位置。
for {
part, err := reader.NextPart()
if err == io.EOF {
break
}
if part.FormName() == "avatar_data" {
// 如何读取“avatar_data”中的内容?
}
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)
}
// 显示成功消息。
}
我没有10个声望来发布POST请求的图像,但它包含以下内容:
<pre>
Content-Disposition: form-data; name="avatar_data"
{"x":528,"y":108,"height":864,"width":864}
</pre>
所以我需要从avatar_data
中获取x
、y
、height
和width
。我知道我需要将JSON进行编组,但我不确定如何达到这一点。
英文:
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
<pre>
Content-Disposition: form-data; name="avatar_data"
{"x":528,"y":108,"height":864,"width":864}
</pre>
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?
答案1
得分: 4
multipart.Part
实现了 io.Reader
接口。
如果 part.FormName()
等于 "avatar_data",则执行以下操作:
j, err := ioutil.ReadAll(part)
if err != nil {
//处理错误
}
//j == []byte(`{"x":528,"y":108,"height":864,"width":864}`),对其进行处理。
英文:
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.
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论