在MultiPartFormData中使用paramteres进行图像上传在Alamofire 4.0中不起作用

时间:2017-02-08 05:25:37

标签: ios swift3 alamofire image-uploading

我正在使用此功能在Alamofire中以post方法上传带有参数的多个图像。我的图片没有上传。我交叉检查服务器上没有图像条目,但其他数据成功存储在DB中。     我在Alamofire 3.0中使用此方法,现在我更新到4.0。其他post和get函数对我来说都很好。

 static func callUploadApi (_ url: String , parameter:[String: String] , images: [URL] , imageParameterName:String ,showHud:Bool , handler: @escaping (_ result : NSMutableDictionary) -> Void )
    {
        Alamofire.upload(multipartFormData: { multipartFormData in

            for (key, value) in parameter  // Add Paramters
            {
                multipartFormData.append(value.data(using: .utf8 )! , withName: key)
            }
            for imageUrl in images // Add Images
            {
                multipartFormData.append(imageUrl, withName: imageParameterName)
            }
        }, to: "\(baseUrl)\(url)", method: .post,
                encodingCompletion: { encodingResult in
                    switch encodingResult {
                    case .success(let upload, _, _):
                        print(upload.progress)

                        upload.responseJSON {  response in

                            if let JSON = response.result.value
                            {
                                print("***********************************************")
                                print("JSON: \(JSON)")
                                print("***********************************************")                                
                                handler(JSON as! NSMutableDictionary)
                            }
                        }
                        break
                    case .failure(let encodingError):
                        print("error:\(encodingError)")
                    }
        })
} 

2 个答案:

答案 0 :(得分:0)

i

答案 1 :(得分:0)

这对我有用,我写这是为了我的需要。任何人都可以根据需要进行更改。

 static func upload( _ url : String ,
                            parameter : [String : String] = [:],
                            arrayImage : [String : UIImage],
                            shouldShowHud : Bool = true,
                            completionHandler : @escaping (_ result : NSDictionary , _ status : Bool) -> Void )
        {
            Alamofire.upload(multipartFormData: { multipartFormData in
                for (key, value) in parameter{
                    multipartFormData.append(value.data(using: .utf8 )! , withName: key)
                }

                for (key, value) in arrayImage {
                    if let imageData = UIImageJPEGRepresentation(imageUrl, 0.5)  {
                        multipartFormData.append(imageData , withName: key, fileName: "file.jpeg", mimeType: "image/jpeg")
                    }
                }
            }, to: MainUrl+url ,  method: .post, headers : Header,  encodingCompletion: { encodingResult in

                switch encodingResult{
                case .success(let upload, _, _):
                    print("Upload Progress \(upload.uploadProgress.completedUnitCount) '\' \(upload.uploadProgress.totalUnitCount)")

                    upload.responseJSON {  response in
                        if let JSON = response.result.value {
                            let status = (JSON as! NSDictionary)["status"] as? Bool
                            completionHandler(JSON as! NSDictionary , status!)
                        } else  {
                            let result : NSDictionary = ["message" : "Request Time out, please refresh again." as Any]
                            completionHandler( result as NSDictionary , false)
                        }
                    }
                    break
                case .failure:
                    let result : NSDictionary = ["message" : "Request Time out, please refresh again." ]
                    completionHandler( result as NSDictionary , false)
                    break
                }
            }
            )
        }