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

问题描述 投票:0回答:2

我试图将多个图像和视频从库上传到网址。 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";
    }
);
}

知道我哪里弄错了吗?

ios swift xcode alamofire multipart
2个回答
1
投票
check below code

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

        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)
        }
    }
)

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)")

        }
    }

}

© www.soinside.com 2019 - 2024. All rights reserved.