0

I am unable to upload images to server getting. Showing upload progress upload.uploadProgress but after upload.responseJSON is nil error. I have tried my best but couldn't solve the issue. Please anyone can help me out. What am I doing wrong? when I debug it { (result) in switch result { result is invalid.

Postman key: (parameters)

ImageList -- for images
ProjectUnitID  -- 8568816

Swift Code:

 if asset.type == .photo {
   let displayImage = asset.fullResolutionImage!images?.append(d)
   let token = UserDefaults.standard.string(forKey: "newToken")
   let image = displayImage
   let imgData = image.jpegData(compressionQuality: 0.2)!
   let parameters = ["ProjectUnitId": 8568816]                 
   Alamofire.upload(multipartFormData: { multipartFormData in
        multipartFormData.append(imgData, withName: "ImageList",fileName: "file.jpg", mimeType: "image/jpg")
        for (key, value) in parameters {
            multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
        } 
   },to:"http://AddProjectUnitImages", headers: [ "Authorization":"Bearer \(String(describing: token))"]) { (result) in
   switch result {
   case .success(let upload, _, _):
       upload.uploadProgress(closure: { (progress) in
           print("Upload Progress: \(progress.fractionCompleted)")
        })

       upload.responseJSON { response in
          print(response.result.value)
       }
       case .failure(let encodingError):
          print(encodingError)
       }
     }
  }
2
  • 1
    Nothing seems to be wrong in your code, check with the backend Code/API Commented Dec 23, 2019 at 9:33
  • @AustinMichael yes I think that too. Commented Dec 23, 2019 at 9:44

2 Answers 2

1

Try this if this is a server error you will get the error here,

Alamofire.upload(multipartFormData: { (multipartFormData) in
    multipartFormData.append(profileImg, withName: "user_image", fileName: "file.jpeg", mimeType: "image/jpeg")
    for (key, value) in parameters {
         multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
    } //}, to:url!,headers:nil)
 }, to:url) { (result) in
 switch result {
 case .success(let upload,_,_ ):
    upload.uploadProgress(closure: { (progress) in
        //Print progress
        print(progress)
    })
    //To check and verify server error
    upload.responseString(completionHandler: { (response) in
        print(response)
        print (response.result)
    })
    upload.responseJSON { response in
       switch response.result {
       case .success:
           print(response)
           completion(response)
       case .failure(let error):
          print(error)
          completion(response)
       }
    }
    case .failure(_):
       print(result)
      // completion(responds)
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Issue was at the back end side. Rectified and now my code works perfectly fine no issues. Thanks everyone.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.