0

i want to upload photo with another parameters i use imagePickerController

internal func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    imagePicked.image = info[UIImagePickerControllerOriginalImage] as? UIImage
    dismiss(animated:true, completion: nil)

}

and i use this function to handle the service but can't work

public static func addCar(photo:UIImage, api_token: String, plate_number: String, type: Int, enddate: String, completion: @escaping (_ error: Error?,_ success: Bool)->Void){

        let parameters = ["api_token": api_token,"plate_number":plate_number,"type": type, "enddate": enddate] as [String : Any]

        let AddCarUrl = APIHelper.BASE_URL + APIHelper.API.ADD_CAR.rawValue

        var jsonData : Data!
        do {
            jsonData = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
            // here "jsonData" is the dictionary encoded in JSON data

            let decoded = try JSONSerialization.jsonObject(with: jsonData, options: [])
            // here "decoded" is of type `Any`, decoded from JSON data

            // you can now cast it with the right type
            if let dictFromJSON = decoded as? [String:String] {
                // use dictFromJSON
                print(dictFromJSON)
            }
        } catch {
            print(error.localizedDescription)
        }

        Alamofire.upload(multipartFormData: { (form: MultipartFormData) in

            form.append(UIImageJPEGRepresentation(photo, 0.5)!, withName: "photo", fileName: "photo", mimeType: "photo/jpeg")
            form.append(jsonData, withName: "data")

            print(jsonData)

        }, usingThreshold: SessionManager.multipartFormDataEncodingMemoryThreshold,
           to: AddCarUrl,
           method: .post,
           headers: nil) { (result: SessionManager.MultipartFormDataEncodingResult) in

            switch result {

            case .success(let upload, _, _):

                upload.uploadProgress(closure: { (progress) in
                    print(progress)
                }).responseJSON(completionHandler: { (response) in

                    switch response.result {
                    case .success(let value):
                        let json = JSON(value)
                        print(json)

                    case .failure(let error):
                        print("Error: ",error)
                    }
                })

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

and i get the error that some data is missing

<NSProgress: 0x60400012cd00> : Parent: 0x0 / Fraction completed: 0.0165 / Completed: 8192 of 497021  

: Parent: 0x0 / Fraction completed: 0.4615 / Completed: 229376 of 497021
: Parent: 0x0 / Fraction completed: 0.6758 / Completed: 335872 of 497021
: Parent: 0x0 / Fraction completed: 0.9065 / Completed: 450560 of 497021
: Parent: 0x0 / Fraction completed: 1.0000 / Completed: 497021 of 497021
{ "code" : "404", "errors" : { "type" : [ "The type field is required." ], "image" : [ "The image field is required." ], "plate_number" : [ "The plate number field is required." ] } }

why i get this error although i upload image and plate_number and type.

can anyone help me.

thanks in advance

mEldewaik
  • 55
  • 1
  • 9
  • Have you tried *breaking down* the functionality into pieces and seeing what's causing the error? You seem to imply that it's related to selecting an image using `UIImagePIckerController` - something you can *easily* eliminate (or give more detail to help us reproduce). Is this related to a particular image and/or size? Don't assume anything - including that you've given us enough detail to reproduce. Give us enough **specifics** to reproduce. Thanks! –  Apr 25 '18 at 09:07
  • 1
    Possible duplicate of [upload image to server using Alamofire](https://stackoverflow.com/questions/40519829/upload-image-to-server-using-alamofire) – Kuldeep Apr 25 '18 at 09:28

0 Answers0