I am trying to hook up image uploading in my application. I am using Alamofire and the multipartFormData
functionality within Alamofire to achieve this.
I have my function written to upload this image along with other attributes.
func savePreferences(parameters: [String:Any], image: UIImage, completion: @escaping (Response?, Error?) -> Void) {
let url = "http://localhost:3000/users/3b4e124d-3b3c-4c71-8e05-013e461c2892"
let imgData = image.jpegData(compressionQuality: 0.5)!
AF.upload(multipartFormData: { multipartFormData in
multipartFormData.append(imgData, withName: "profile_imgname",fileName: "ProfilePic_\(UserDefaults.standard.string(forKey: "user_id") ?? "").jpg", mimeType: "image/jpg")
for (key, value) in parameters {
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
}, to: url, method: .patch) { (result) in
switch result {
case .success(let upload):
upload.responseJSON { response in
if let err = response.error {
failure(err)
return
}
completion(response.result.value)
}
case .failure(let error):
print("Error in upload: \(error.localizedDescription)")
}
}
}
In the switch
statement at the end, in the case .success
and case .failure
, I am receiving the following error:
Type 'URLRequest' has no member 'failure'
Type 'URLRequest' has no member 'success'
I have used existing Stack Overflow resources to understand how this multipartFormData
works, and it seems that they follow this format to an extent.
Please see below:
Send POST parameters with MultipartFormData using Alamofire, in iOS Swift
How to upload pictures with Alamofire (post)?
Why am I getting the errors with URLRequest has no member failure
and how can I correct this?