dongqin6926 2018-04-05 08:17
浏览 52
已采纳

如何在golang中从客户端接收多部分内容?

Here my code and output:

func RestClient(req *http.Request) {
        fmt.Println("main (120):::", req.MultipartForm.File)
}

main (120)::: &{map[userName:[0xc4200a66e0] diamond:[0xc4200f67b0] ]}

for k,v := range req.MultipartForm.File{
              if k == "userName" {
                for _, v2 := range v {
                    fmt.Println("main (130):::",v2)
                } 
              }     
           }
 > main (130)::: &{ map[Content-Length:[8]
    > Content-Disposition:[form-data; name="dk"]
    > Content-Transfer-Encoding:[binary] Content-Type:[multipart/form-data;
    > charset=utf-8]] 8 [117 115 101 114 78 97 109 101] }

I want the (slice byte [117 115 101 114 78 97 109 101]) but can’t pull out that, how can print the (content []byte), As we know the field from FileHeader struct:

type FileHeader struct {
    Filename string
    Header   textproto.MIMEHeader
    Size     int64

    content []byte
    tmpfile string
}

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • doupafu6980 2018-04-05 10:30
    关注

    Call FileHeader.Open, which returns a multipart.File:

    type File interface {
            io.Reader
            io.ReaderAt
            io.Seeker
            io.Closer
    }
    

    How you deal with this file depends on what you want to do with the contents. ioutil.ReadAll is an option, but often it is more convenient to leave it as-is and use io.Copy to write the content to another io.Writer. Don't forget to call Close after you're done reading.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘