Issue with uploading an image using Alamofire(has image as a parameter)

This is how I am trying to upload an image using Alamofire. But the program crashes saying something like…'NSInvalidArgumentException', reason: '-[_SwiftTypePreservingNSNumber dataUsingEncoding:]: unrecognized selector sent to instance... I’m not able figure the exact reason.This is how I’m making the request…

    for i in 1...(imageArray.count) {
        for img in imageArray {

            let url = ""
            let headers = [ "Content-Type":"application/x-www-form-urlencoded"]

  let imageData: Data = (UIImageJPEGRepresentation(img, 0.6) as Data?)!

            let parameters: [String: Any]  = [
                    "access_token":  commonVarForAccessToken,
                    "seller_id": idForNewOldUser, 
                    "product_id": self.productId,
                    "is_default": "1",
                    "sequence": i,
                    "image": imageData  ]

            Alamofire.upload(multipartFormData: { (multipartFormData) in

                multipartFormData.append(imageData as Data, withName: "home-\(self.index)", fileName: "home-\(self.index)", mimeType: "image/jpeg")

                for (key, value) in parameters {

                    multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)

            }, to:url)
            { (result) in
                switch result {
                case .success(let upload, _, _):
                    upload.uploadProgress(closure: { (Progress) in
                        //Print progress
                    upload.responseJSON { response in

                        print(response.request)  // original URL request

                         if let JSON = response.result.value {
                            print("JSON: \(JSON)")

                case .failure(let encodingError):


Hope somebody can help…Thanks…:)

  • Text not displaying on UILabel when the text is too large.
  • NSTimer stops in the background after some time
  • NSMutableURLRequest and “request body stream exhausted” error
  • Xcode 4 template, create empty group
  • Retrieve Facebook friends list
  • Core Data with Array Attribute
  • Solutions Collect From Internet About “Issue with uploading an image using Alamofire(has image as a parameter)”

    Try to use this code.It’s working for me.

    let para: [String: Any]
    Alamofire.upload(multipartFormData: {(multipartFormData) in
    for i in 0..<uploadImages.count{         multipartFormData.append(UIImageJPEGRepresentation(uploadImages[i], 0.3)!, withName: "image\(i)", fileName: "swift_file\(i).jpeg", mimeType: "image/jpg")             
            for (key, value ) in para {
                multipartFormData.append((value).data(using: String.Encoding.utf8)!, withName: key)
        }, to: apiURL)
        { (result) in
            switch result {
            case .success(let upload, _,_ ):
                upload.uploadProgress(closure: { (progress) in
                    UILabel().text = "\((progress.fractionCompleted * 100)) %"
                    print (progress.fractionCompleted * 100)
                upload.responseJSON { response in
                    guard ((response.result.value) != nil) else{