如何创建一个包含多个FileHeaders的HTTP请求?

huangapple go评论74阅读模式
英文:

How to create a http request that contains multiple FileHeaders?

问题

我正在尝试测试一个支持多文件上传的上传服务,并找到了这个链接:https://stackoverflow.com/questions/20205796/golang-post-data-using-the-content-type-multipart-form-data,它介绍了如何创建一个上传单个文件的请求,但我需要上传多个文件,有没有简单的方法来创建这种类型的请求?

更新:

请检查文章中的第38行和39行:支持HTML5多文件上传


line 38    files := m.File["myfiles"]
line 29      for i, _ := range files {

似乎需要为多个文件头设置单个名称以模拟HTML5多文件上传。

英文:

I am trying to test a uploading service that supports multiple files uploading,and I found this:

https://stackoverflow.com/questions/20205796/golang-post-data-using-the-content-type-multipart-form-data

that introduced how to create a request to upload a single file,but I need to upload multiple files,is there simple way to create this kind of request?

update:

please check line:38 and 39 in post:to support html5 multiple files uploading


line 38    files := m.File["myfiles"]
line 29      for i, _ := range files {

It seems that it needs to set single name for multiple file headers to stimulate the html5 multiple files uploading.

答案1

得分: 3

对于每个文件,调用CreateFormFile来创建文件的头部。从CreateFormFile返回的writer上调用Write一次或多次来写入文件的数据。当所有文件处理完毕后,调用multipart writer的close方法。

链接的问题中,排名第一的答案上传了两个文件,一个名为"image",另一个名为"key"。"image"的数据是从一个文件中复制而来,而"key"的数据则是简单的字节"KEY"。

字段名是传递给CreateFormFile的第一个参数。如果你想上传多个具有相同名称的文件,每次调用CreateFormFile时都使用相同的名称。

英文:

For each file, call CreateFormFile to create the header for the file. Call Write on the writer returned from CreateFormFile one or more times to write data to the file. When done with all files, close the multipart writer.

The top answer in the linked question uploads two files, one named "image" and one named "key". The data for the "image" is copied from a file. The data for "key" is simply the bytes "KEY".

The field name is the first argument to CreateFormFile. If you want to upload multiple files with the same name, use the same name each time you call CreateFormFile.

huangapple
  • 本文由 发表于 2014年9月26日 23:38:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/26063271.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定