单个文件获取
      tapUploadPic() {
          wx.uploadFile({
              filePath: this.data.files[0],
              name: 'file',
              url: 'http://localhost:8080/upload',
              // 跟随文件一起上传时候,传给后端的文字数据
              formData: {
                  'Message': 'upload Picture'
              },
              // header形式必须一样
              header: {
                  "Content-Type": "multipart/form-data"
              },
              success(res) {
                  console.log("Success")
                  // 此处会在console打印后端回传的信息
                  console.log(res)
              },
              fail(res) {
                  console.log("Fail")
                  console.log(res)
              }
          })
      }
参考链接:GIN文件上传与返回
  package main
  
  import (
  	"fmt"
  	"github.com/gin-gonic/gin"
  	"strconv"
  )
  
  // 取消注释即可运行
  func main() {
  	testSinglePic()
  }
  
  func testSinglePic() {
  	r := gin.Default() //启动gin路由,携带基础中间件启动 logger and recovery (crash-free) 中间件
  	r.POST("/upload", func(c *gin.Context) {
  		// 获取c中的文件流,名字一定要和前端的名字一致(比如上面的name),否则会报错:http: no such file
  		file, err := c.FormFile("file")
  		// 获取前端返回回来的文字数据并打印到console中
  		message := c.PostForm("Message")
  		fmt.Println(message)
  		Check(err)
  		// 返回信息到前端:JSON形式
  		c.JSON(200, gin.H{
  			"msg": "Post Pic Okay",
  		})
  		// 将回传的文件保存下来,第一个参数是要保存的名字,和上面的FormFile相对应,
  		// 第二个参数是存储的路径,./example.png,就是存储在本目录下,重命名为example.png,如果没有后缀则无法打开,丢失文件格式信息
  		//err = c.SaveUploadedFile(file, "./example.png")
  
       // 存到上一层录Wechat中的PostPic文件夹中,其中file.Filename包含文件后缀信息:'xxxx.png’(示例)
       // 如果还想存到再上一层,只需要前面加上:‘../’,即可
       // 路径必须要正确,否则会报错,如果使用的不是指向文件夹的路径,会在文件名前面加上前缀保存
       // 比如:"../Wechat/PostP"+file.Filename(假设为:xxx.png),会放在Wechat文件夹下面,并且文件名会变成"PostPxxx.png"
  		err = c.SaveUploadedFile(file, "../Wechat/PostPic/"+file.Filename)
          // 打印信息:文件名称(string)、文件大小(int64)、文件信息(map)
  		fmt.Println("file.tmpfile: " + file.Filename)
  		fmt.Println("file.Size: " + strconv.FormatInt(file.Size, 10))
  		fmt.Println("file.Header: ", file.Header)
  		Check(err)
  	})
  	r.Run(":8080") // listen and serve on 0.0.0.0:8080
  }
附上file结构体的源码:
type FileHeader struct {
	Filename string
	Header   textproto.MIMEHeader
	Size     int64
	content []byte
	tmpfile string
}
批量文件
获取
- 这里我使用的是
airpost6进行传输数据调试,具体传输的数据为: 
file : ["文件1","文件2"]
Message : "Multipart files upload"
- header一定要是
"Content-Type": "multipart/form-data" 
- 其实批量文件与单个文件是差不多一样的,具体差距看注释
 
后端代码:
package main
import (
	"github.com/gin-gonic/gin"
	"net/http"
)
func main() {
	multipartManage()
}
func multipartManage() {
	r := gin.Default() //启动gin路由,携带基础中间件启动 logger and recovery (crash-free) 中间件
	r.POST("/MultipartUpload", func(c *gin.Context) {
		form, err := c.MultipartForm()
		Check(err)
		// 获得文件流中名为file的批量文件数组
		file := form.File["file"]
		// 通过循环存储文件,每一次遍历都相当于处理单个文件
		for _, f := range file {
			c.SaveUploadedFile(f, "../Wechat/PostPic/"+f.Filename)
		}
		c.JSON(http.StatusOK, gin.H{
			"msg": "Multipart upload Success!",
		})
	})
	r.Run(":8080") // listen and serve on 0.0.0.0:8080
}
其中需要说明一下的就是form的结构:
// Form is a parsed multipart form.
// Its File parts are stored either in memory or on disk,
// and are accessible via the *FileHeader's Open method.
// Its Value parts are stored as strings.
// Both are keyed by field name.
type Form struct {
	Value map[string][]string
	File  map[string][]*FileHeader
}
其中File的结构FileHeader和单个文件上传的结构体是一致的,这就不难理解其实单个文件上传和多个文件上传是差不多一样的原理,都是已经封装好了,十分方便使用