How to send array in params using Alamofire multipart

ε祈祈猫儿з 提交于 2019-12-02 03:30:45

You need to pass image parameter along with your other request parameters. Pass your array parameters like this in below code:

Alamofire.upload(
            multipartFormData: { multipartFormData in
                // Pass your image parameter in imgObj
                if  let imageData = UIImageJPEGRepresentation(imgObj, 1) {                        
                    multipartFormData.append(UIImagePNGRepresentation(imgObj)!, withName: "profile_image", fileName: "THDC", mimeType: "image/png")
                }
                // Send other request parameters
                for (key, value) in yourArray {
                    multipartFormData.append((value as! String).data(using: .utf8)!, withName: key)
                }
        },to: YourURL,headers:[:],
          encodingCompletion: { encodingResult in

            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    SVProgressHUD.dismiss()
                    debugPrint("SUCCESS RESPONSE: \(response)")

                    if let dicObj = response.result.value as? NSDictionary {
                        print(dicObj)

                        }
                }
            case .failure(let encodingError):
                SVProgressHUD.dismiss()
                print("ERROR RESPONSE: \(encodingError)")
            }
        }
        )
Sandeep Baddula

This is the static way to upload arrays to Alamofire. hope this may useful to you.

Alamofire.upload(multipartFormData: { (multipartFormData) in

            let imageData = UIImageJPEGRepresentation(imageUpload!, 0.5)

            multipartFormData.append(imageData!, withName: "profile_file", fileName: "file.png", mimeType: "image/jpg")

            for (key, value) in parameters {
                if  (value as AnyObject).isKind(of: NSMutableArray.self)
                {
                    let arrayObj = value as! NSMutableArray
                    //let data2 = NSData(bytes: &arrayObj, length: arrayObj.count)

                    let count : Int  = arrayObj.count

                    for i in 0  ..< count
                    {

                        let value = arrayObj[i] as! Int
                        let valueObj = String(value)

                        let keyObj = key + "[" + String(i) + "]"

                        multipartFormData.append(valueObj.data(using: String.Encoding.utf8)!, withName: keyObj)
                    }


                }
                else{
                    var valueStr = String()
                    if let param = value as? String{
                        valueStr = param
                    }else{
                        let valueInt = value as! Int
                        valueStr = String(valueInt)
                    }

                    multipartFormData.append((valueStr).data(using: String.Encoding.utf8)!, withName: key)
                }


            }



            }, to: urlString, encodingCompletion: { (encodingResult) in

                print("=====encodingResult=========",encodingResult)
                switch encodingResult {
                case .success(let upload, _, _):

                    upload.responseJSON(completionHandler: { (response) -> Void in


                        switch response.result {
                        case .success(let JSON):
                            print("JSON: \(JSON)")
                            onCompletion(JSON as? NSDictionary, nil)

                        case .failure(let error):
                            print(error)

                        }


                    })

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


        })
Sazid Iqabal

You need to append array with multipart data on the same key required, like in your code you need to change only given line of code:

  for (key, value) in params
        {
             // check the key on which key array is coming
            if key == "arrayParam" {

               let arrData =  try! JSONSerialization.data(withJSONObject: value, options: .prettyPrinted)
                multipartFormData.append(arrData, withName: key as String)
            }
            else {
                multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
            }

        }

Rest will be the same.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!