无法使用Alamofire从阵列上传多个文件

时间:2018-09-04 20:49:37

标签: ios swift xcode alamofire multipart

我正在尝试将多个图像和视频从库上传到url。 evertying可以正常工作,除了,响应只打开一个文件。我为实验选择了两个不同的图像,但响应仅针对数组中的最后一个图像。这是我的上传函数。

 func upload() {
    var mediaData = Data()
    var fname = ""
    var mimetype = ""
    let url = URL(string: "https://myurl.com/api/attachment/upload")
        let headers: HTTPHeaders = [
            "Authorization": "my Authorization Token",
            "Content-type": "multipart/form-data"
        ]

        Alamofire.upload(multipartFormData: { multipartFormData in
               for n in 0..<self.array.count{
                    mediaData = self.array[n].data
                    if self.array[n].type == "image"{
                        fname = "resim\(n).jpeg"
                        mimetype = "image/jpeg"
                    }else{
                        fname = "video\(n).mp4"
                        mimetype = "video/mp4"
                    }
                    multipartFormData.append(mediaData, withName: "files", fileName: fname, mimeType:mimetype)
                }


        },
          to: url!, method: .post, headers: headers) { (result) in
            switch result{
            case .success(let upload, _, _):
                upload.uploadProgress(closure: { (progress) in

                    print("uploding: \(progress.fractionCompleted)")
                })


                upload.responseJSON{ response in
                    print(response)
                    self.hud.dismiss()

                    if let err = response.error{
                        print(err)
                        return
                    }

                }
            case .failure(let error):
                print("Error in upload: \(error.localizedDescription)")

            }
        }
}

这就是服务器作为响应返回的内容。

SUCCESS: {
files =     (
            {
        "local_name" = BaN9x05lU8jNQPJ30RfbT4rqismLp7LE45sHezHvI;
        "real_name" = "resim1.jpeg";
    }
);
}

任何想法我在哪里弄错了?

2 个答案:

答案 0 :(得分:1)

check below code

var dataArray = [(“名称”:“ unicorn1”,“ imageData”:imgData1),(“名称”:“ unicorn2”,“ imageData”:imgData2)]     Alamofire.upload(         multipartFormData:{在

中为multipartFormData
        for dat in dataArray {

           multipartFormData.append(dat.imageData, withName: dat.name)
        }

    },
    to: "https://httpbin.org/post",
    encodingCompletion: { encodingResult in
        switch encodingResult {
        case .success(let upload, _, _):
            upload.responseJSON { response in
                debugPrint(response)
            }
        case .failure(let encodingError):
            print(encodingError)
        }
    }
)

答案 1 :(得分:0)

我解决了将"files[\(n)]"更改为

的一部分的问题
 multipartFormData.append(mediaData, withName: "files", fileName: fname, mimeType:mimetype)

然后问题解决了。

func upload() {
var mediaData = Data()
var fname = ""
var mimetype = ""
let url = URL(string: "https://myurl.com/api/attachment/upload")
    let headers: HTTPHeaders = [
        "Authorization": "my Authorization Token",
        "Content-type": "multipart/form-data"
    ]

    Alamofire.upload(multipartFormData: { multipartFormData in
           for n in 0..<self.array.count{
                mediaData = self.array[n].data
                if self.array[n].type == "image"{
                    fname = "resim\(n).jpeg"
                    mimetype = "image/jpeg"
                }else{
                    fname = "video\(n).mp4"
                    mimetype = "video/mp4"
                }
                multipartFormData.append(mediaData, withName: "files[\(n)]", fileName: fname, mimeType:mimetype)
            }


    },
      to: url!, method: .post, headers: headers) { (result) in
        switch result{
        case .success(let upload, _, _):
            upload.uploadProgress(closure: { (progress) in

                print("uploding: \(progress.fractionCompleted)")
            })


            upload.responseJSON{ response in
                print(response)
                self.hud.dismiss()

                if let err = response.error{
                    print(err)
                    return
                }

            }
        case .failure(let error):
            print("Error in upload: \(error.localizedDescription)")

        }
    }

}