上传多张图片(Alamofire)

时间:2018-08-24 09:09:08

标签: swift image file-upload upload alamofire

我有一个小问题。

我想使用Alamofire将多个图像发送到服务器,但出现错误..代理告诉我:“请求无效”

我的代码在这里:

func uploadImage(TabImage: [UIImage]){

    let urlReq = "http://www.myurl.com/upload.php"
    let parameters = ["id": my_id]

    Alamofire.upload(multipartFormData: { multipartFormData in

        var i=0
        for imageActuel in TabImage {
            let tmp_image_data = UIImagePNGRepresentation(imageActuel)
            let name = "exemple_"+ my_id +"_"+String(i)+"[]"
            let filename = "exemple_"+ my_id +"_"+String(i)+".jpg"
            multipartFormData.append(tmp_image_data!, withName: name,fileName: filename, mimeType: "image/jpg")
            i+=1
        }

        for (key, value) in parameters {
            multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
        }
    },to:urlReq)
    { (result) in
        switch result {
        case .success(let upload, _, _):

            upload.uploadProgress(closure: { (progress) in
                print("Upload Progress: \(progress.fractionCompleted)")
            })

            upload.responseJSON { response in
                print(response.result.value)
                if let dic = response.result.value as? NSDictionary{
                    print(dic)
                }
            }

        case .failure(let encodingError):
            print(encodingError)
        }
    }


}

您在此功能中看到问题了吗?

感谢您的帮助:)

0 个答案:

没有答案