#### 请求
![image.png](https://static.studygolang.com/190221/e93397905543fa01abcef5c685b5e277.png)
#### 代码如下
```
//文件批量上传
func SFansUploadFileMore(c *gin.Context) ([]string, error) {
folder := c.PostForm("path")
if folder == "" {
return nil, fmt.Errorf("未命名文件上传主目录")
}
up := make(chan FileMoreChan)
isStart := make(chan bool)
defer func() {
close(up)
close(isStart)
}()
form, _ := c.MultipartForm()
files := form.File["file[]"]
fileS := make([]string, 0)
//启动
go func() {
isStart <- true
}()
go func() {
for _, file := range files {
select {
case b := <-isStart:
if b {
go func(file *multipart.FileHeader) {
f, err := fansUploadFile(file, folder)
up <- FileMoreChan{f["mainUrl"], err}
}(file)
}
}
}
}()
//接收up结构
for upC := range up {
if upC.Err != nil {
for _, url := range fileS {
//有上传成功得文件,则删除
ext.QiniuDelUrl(url)
}
isStart <- false
return nil, logs.SysErr(upC.Err)
} else {
//所接收文件没有错误提示
fileS = append(fileS, upC.Url)
if len(fileS) != len(files) {
isStart <- true
} else {
break
}
}
}
return fileS, nil
}
//粉丝文件上传
func fansUploadFile(file *multipart.FileHeader, folder string) (map[string]string, error) {
path, err := ext.QiniuUpload(file, folder)
if err != nil {
return nil, err
}
urls := make(map[string]string)
urls["thumb"] = fmt.Sprintf(e.ThumbUrl, path)
urls["mainUrl"] = path
return urls, nil
}
```
#### 返回结果
```
{
"code": 200,
"data": [
"http://fans.dcwen.top/fans-test/ai/7a592ba18ea3330b10e4e0de8ffb0ecc.png"
],
"error": 0
}
```
#### 如果有不够完善的地方,欢迎大家指教
有疑问加站长微信联系(非本文作者)