0

I am using moya and want to send image, video & parameters dictionary of [String : Any]

for parameters I am writing

*for (key, value) in param {
            multipartFormData.append(Moya.MultipartFormData(provider: .data((value as AnyObject).data(using: String.Encoding.utf8.rawValue) ?? Data()) , name : key))
            }*

but i am getting this error as my param dictionary contains String & Int as well

*-[__NSCFNumber dataUsingEncoding:]: unrecognized selector sent to instance*

Please suggest how can i resolve this?

1 Answers1

0

to add image inside multipartFormData you could try something like this line.

  MultipartFormData.append(UIImageJPEGRepresentation(UIImage(named: "1.png")!, 1)!, withName: "photos[1]", fileName: "swift_file.jpeg", mimeType: "image/jpeg")

using Alamofire a full example is something like this

let parameters = [
            "param1" :        "1000",
            "param2":      "Murat Akdeniz",
            "param3":        "xxxxxx"]

let imgData = UIImageJPEGRepresentation(UIImage(named: "1.png")!,1)



    Alamofire.upload(
        multipartFormData: { MultipartFormData in
        //    multipartFormData.append(imageData, withName: "user", fileName: "user.jpg", mimeType: "image/jpeg")

            for (key, value) in parameters {
            let value = "\(value)"  //Added this line to use [String:Any ] param types

                MultipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
            }

    MultipartFormData.append(UIImageJPEGRepresentation(UIImage(named: "1.png")!, 1)!, withName: "photos[1]", fileName: "swift_file.jpeg", mimeType: "image/jpeg")
    MultipartFormData.append(UIImageJPEGRepresentation(UIImage(named: "1.png")!, 1)!, withName: "photos[2]", fileName: "swift_file.jpeg", mimeType: "image/jpeg")


}, to: "youURL") { (result) in

    switch result {
    case .success(let upload, _, _):

        upload.responseJSON { response in
            print(response.result.value)
        }

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


}
Mohmmad S
  • 5,001
  • 4
  • 18
  • 50