I am using alamofire 4.7 and swift 4
I need to upload image and json to server.
I am using the following code below for uploading bu I am getting result failure but data is inserting in server but not getting response, showing some serialization error as something like this
▿ result : FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))
▿ failure : AFError
▿ responseSerializationFailed : ResponseSerializationFailureReason
▿ timeline : Timeline: { "Request Start Time": 548835779.066, "Initial Response Time": 548835779.074, "Request Completed Time": 548835779.127, "Serialization Completed Time": 548835779.127, "Latency": 0.008 secs, "Request Duration": 0.061 secs, "Serialization Duration": 0.000 secs, "Total Duration": 0.061 secs }
- requestStartTime : 548835779.06617701
- initialResponseTime : 548835779.07390201
- requestCompletedTime : 548835779.12704694
- serializationCompletedTime : 548835779.12748504
- latency : 0.0077250003814697266
- requestDuration : 0.060869932174682617
- serializationDuration : 0.00043809413909912109
- totalDuration : 0.061308026313781738
▿ _metrics : Optional<AnyObject>
=================================================================
let auth : String = MitraModal.sharedInstance().getBasicAuthenticationString()
let headers = ["Authorization": auth, "Content-type": "multipart/form-data"]
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append("\(parameters)".data(using: String.Encoding.utf8)!, withName: "data" as String)
if (!imageArray.isEmpty) {
for item in imageArray {
multipartFormData.append(item!, withName: "file", fileName: "image.png", mimeType: "image/png")
}
}
}, usingThreshold: UInt64.init(), to: url, method: .post, headers: headers) { (result) in
switch result{
case .success(let upload, _, _):
upload.responseJSON { response in
if let JSON = response.result.value
{
print("JSON: \(JSON)")
onCompletion?(JSON as? JSON)
print("Successfully uploaded")
}
if let err = response.error {
onError?(err)
return
}
onCompletion?(nil)
}
case .failure(let error):
print("Error in upload: \(error.localizedDescription)")
onError?(error)
}
}
}
Anyone help ?
As i'm newbie, but Few days ago, i had same problem while uploading image, you must have to accept image by using file on the server side because of your image tag consist withName: "file" .
func AlamofireUploadImages(){
let url : String = URL_IP+"/ZameenServer/api/addNewProperty.php"
for img in HOUSEImages{
let data = UIImageJPEGRepresentation(img, 0.2)!
ImagesData.append(data)
}
let parameters = [
"userId" : "5",
"unit" : "6789"
] //Optional for extra parameter
Alamofire.upload(multipartFormData: { multipartFormData in
for imageData in self.ImagesData {
multipartFormData.append(imageData, withName: "file[]", fileName: self.randomString(length: 5)+".jpeg", mimeType: "image/jpeg")
}
for (key, value) in parameters {
multipartFormData.append((value?.data(using: String.Encoding.utf8)!)!, withName: key)
} //Optional for extra parameters
},
to:url)
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})
upload.responseJSON
{
response in
print("Response :\(response.result.value)")
}
case .failure(let encodingError):
print("no Error :\(encodingError)")
}
}
}
NOTE: As i'm uploading array of images so for uploading multile images use withName: "file[]" or for single image use withName: "file"
Might be it helps you.
Thanks
let url = BaseViewController.API_URL + "uploads"
let image = info[UIImagePickerControllerEditedImage] as? UIImage
let imgData = UIImageJPEGRepresentation(image!, 0.2)!
let parameters = [
"user_id" : UserDefaults.standard.value(forKey: "userId")!
]
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(imgData, withName: "uload_data",fileName: "file.jpg", mimeType: "image/jpg")
for (key, value) in parameters {
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
} //Optional for extra parameters
},
to:url)
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})
upload.responseJSON { response in
self.objHudHide()
print(response.result.value)
let jsonDict : NSDictionary = response.result.value as! NSDictionary
print(jsonDict)
if jsonDict["status"] as! String == "Success"
{
let detailDict : Dictionary = jsonDict["detail"] as! Dictionary<String,Any>
if let getTotalPrice = detailDict["total_price"]
{
self.lblTotalPrice.text = "$ \(getTotalPrice) + Free Shipping"
}
if let getTotalSize = detailDict["total_upload_size"]
{
self.lblTotalSize.text = "Total Size : \(getTotalSize)"
}
}
else
{
let alertViewController = UIAlertController(title: NSLocalizedString("Alert!", comment: ""), message:"Something Went wrong please try again." , preferredStyle: .alert)
let okAction = UIAlertAction(title: NSLocalizedString("Ok", comment: ""), style: .default) { (action) -> Void in
}
alertViewController.addAction(okAction)
self.present(alertViewController, animated: true, completion: nil)
}
}
case .failure(let encodingError):
print(encodingError)
}
}
I get the similar issue as well. This may cause several reasons. In my case that was the error of parameter key of the POST request. When you are executing following line
multipartFormData.append(item!, withName: "file", fileName: "image.png", mimeType: "image/png")
withName:"file" property refers the parameter key (key of the Json request) for the image of the POST request. So you can't use whatever the text you want here. Please add the correct parameter key of the image that your API is referring instead of "file". Let's say API access the image using "fileImage" as the parameter key. So the above line should be something like this.
multipartFormData.append(item!, withName: "fileImage", fileName: "image.png", mimeType: "image/png")
Related
i try many solution but give always this error when upload image on server using alomafire
Trailing closure passed to parameter of type 'FileManager' that does not accept a closure
let params: Parameters = ["name": "abcd","gender": "Male", "hobbies" : HobbyArray]
AF.upload(multipartFormData:
{
(multipartFormData) in
multipartFormData.append(UIImageJPEGRepresentation(self.yourimageView.image!, 0.1)!, withName: "image", fileName: "file.jpeg", mimeType: "image/jpeg")
for (key, value) in params
{
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
}, to: "\(BaseUrl)/save-beers" , headers:nil)
{ (result) in
switch result {
case .success(let upload,_,_ ):
upload.uploadProgress(closure: { (progress) in
//Print progress
})
upload.responseJSON
{ response in
//print response.result
if response.result.value != nil
{
let dict :NSDictionary = response.result.value! as! NSDictionary
let status = dict.value(forKey: "status")as! String
if status=="1"
{
print("DATA UPLOAD SUCCESSFULLY")
}
}
}
case .failure(let encodingError):
break
}
}
You can use this code for uploading Image
//MARK: - Upload image using alamofire with multiparts
func uploadImageAPIResponse(postPath:String,img: UIImage, parameters:NSDictionary, requestType: RequestType){
let imgData = img.jpegData(compressionQuality: 0.5)!
let headers: HTTPHeaders = ["Authorization": "Your_Auth if any otherwise remove Header from request"]
print("postPath:\(postPath)\nheaders:\(headers)\nparameters: \(parameters)")
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(imgData, withName: "file",fileName: "profile.jpg", mimeType: "image/jpg")
for (key, value) in parameters {
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key as! String)
} //Optional for extra parameters
},
to:postPath)
{ (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 as Any)
if response.result.isSuccess
{ let httpStatusCode: Int = (response.response?.statusCode)!
let data = (response.result.value as? NSDictionary)!
let meta = (data["meta"] as? NSDictionary)!
let code = meta["code"] as? Int ?? 0
print(data)
//if (data["success"] as? Bool)! {
print("httpCode" + String(httpStatusCode))
print("code" + String(code))
switch(httpStatusCode) {
case 200://operation successfull
if code == 401 {
let deleg = UIApplication.shared.delegate as! AppDelegate
User.defaultUser.logoutUser()
deleg.showLoginScreen()
}
self.delegate?.requestFinished(responseData: data, requestType: requestType)
break
case 204://no data/content found
self.delegate?.requestFinished(responseData: data, requestType: requestType)
break
case 401://Request from unauthorized resource
self.delegate?.requestFailed(error: "Request from unauthorized resource", requestType: requestType)
break
case 500://Internal server error like query execution failed or server script crashed due to some reason.
self.delegate?.requestFailed(error: "Internal server error like query execution failed or server script crashed due to some reason.", requestType: requestType)
break
default:
self.delegate?.requestFinished(responseData: data, requestType: requestType)
break
}
}
else
{
self.delegate?.requestFailed(error: (response.result.error?.localizedDescription)!, requestType: requestType)
}
}
case .failure(let encodingError):
print(encodingError)
self.delegate?.requestFailed(error: encodingError.localizedDescription, requestType: requestType)
}
}
}
I'm currently working on iOS applications and I'm using multipart image upload to uploading images to the server. Following is my image uploading method.
func uploadImageData(imageType:Int, uploadId:String, fileName:String, imageFile:UIImage, completion:#escaping (APIResponseStatus, ImageUploadResponse?) -> Void) {
let image = imageFile
let imgData = image.jpegData(compressionQuality: 0.2)!
let params = [APIRequestKeys.imageType:imageType, APIRequestKeys.uploadId:uploadId, APIRequestKeys.fileName:fileName] as [String : Any]
//withName is the post request key for the image file
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(imgData, withName: APIRequestKeys.imageFile, fileName: "\(fileName).jpg", mimeType: "image/jpg")
for (key, value) in params {
multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key)
}
}, to: Constants.baseUrl + APIRequestMetod.uploadImageData, headers:self.getImageUploadHeaders())
{ (result) in
switch result {
case .success(let upload, _, _):
APIClient.currentRequest = upload
upload.uploadProgress(closure: { (progress) in
})
upload.responseObject {
(response:DataResponse<ImageUploadResponse>) in
switch response.result {
case .success(_):
completion(APIClient.APIResponseStatus(rawValue: (response.response?.statusCode)!)!, response.value!)
case .failure(let encodingError):
if let err = encodingError as? URLError, err.code == .notConnectedToInternet {
completion(APIClient.APIResponseStatus.NoNetwork, nil)
} else {
completion(APIClient.APIResponseStatus.Other, nil)
}
}
}
case .failure( _):
completion(APIClient.APIResponseStatus.Other, nil)
}
}
}
But for this implementation server is always sending exif data error. Following is the error that I'm getting.
exif_read_data(A029C715-99E4-44BE-8691-AA4009C1F5BD_FOTOPREGUNTA.ico): Illegal IFD size in
upload_image_xhr.php on line
The important thing is this service is working without errors in POSTMAN and android application as well. This error is only getting for my iOS implementation. My backend developer telling me that there is and exif data error in data that I'm sending and please verify the data from my side.
Anyone have an idea about this?
Thanks in advance.
I will make block function for Upload image to Server Using Multipart
//Here strUrl = YOUR WEBSERVICE URL
//postParam = post Request parameter i.e.
//let postParam : [String : Any] = [first_name : "name"]
//imageArray = image upload array i.e.
//var imageArray : [[String:Data]] = [["image_name" : YOUR IMAGE DATA]]
func postImageRequestWithURL(withUrl strURL: String,withParam postParam: Dictionary<String, Any>,withImages imageArray:[[String:Data]], completion:#escaping (_ isSuccess: Bool, _ response:NSDictionary) -> Void)
{
let requetURL = strURL
Alamofire.upload(multipartFormData: { (MultipartFormData) in
for (imageDic) in imageArray
{
for (key,valus) in imageDic
{
MultipartFormData.append(valus, withName:key,fileName: "file.jpg", mimeType: "image/jpg")
}
}
for (key, value) in postParam
{
MultipartFormData.append("\(value)".data(using: .utf8)!, withName: key)
// MultipartFormData.append(value, withName: key)
}
}, usingThreshold: UInt64.init(), to: requetURL, method: .post, headers: ["Accept": "application/json"]) { (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})
upload.responseJSON { response in
let desiredString = NSString(data: response.data!, encoding: String.Encoding.utf8.rawValue)
print("Response ====================")
print(desiredString!)
if let json = response.result.value as? NSDictionary
{
if response.response?.statusCode == 200
|| response.response?.statusCode == 201
|| response.response?.statusCode == 202
{
completion(true,json);
}
else
{
completion(false,json);
}
}
else
{
completion(false,[:]);
}
}
case .failure(let encodingError):
print(encodingError)
completion(false,[:]);
}
}
}
I Hope this will help...
I am trying to upload image with multiple parameters using alamofire multipart with swift 4 but I can not upload image sucessfully and I got response like
{
"error" : "error uploading file, please retry"
}
Here is my function which I call on upload button event.
// selectedLogo = (info["UIImagePickerControllerOriginalImage"] as? UIImage)!
let imageData = UIImageJPEGRepresentation(selectedLogo, 1.0)
let parameters: Parameters = ["id": strUserId,
"telephone": self.txtTelephoneNumber.text!,
"email": self.txtEmail.text!,
"notice":self.txtNotices.text!,
"status" : userData["status"]]
print(parameters)
Alamofire.upload(multipartFormData: { (multipartFormData) in
if let data = imageData{
multipartFormData.append(data, withName: "club_image", fileName: "file.jpg", mimeType: "image/jpg")
}
for (key, value) in parameters {
multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!,withName: key as String)
}
}, to:"\(self.app.strBaseAPI)updatedata.php")
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
//Print progress
print(progress)
})
upload.validate()
upload.responseJSON { response in
if response.response?.statusCode == 200
{
if response.result.isSuccess == true
{
if let value = response.result.value
{
self.json = JSON(value)
print(self.json)
let strStatus : String = self.json["success"].stringValue
if strStatus == "true"
{
Toast(text: strStatus).show()
}else{
Toast(text: strStatus).show()
}
}else{
Toast(text: "Something wrong").show()
}
}else{
Toast(text: "Something wrong").show()
}
}else{
SVProgressHUD.dismiss()
Toast(text: "Something wrong").show()
}
}
case .failure(let encodingError):
//print encodingError.description
print(encodingError.localizedDescription)
}
when I convert image using UIImagePNGRepresentation with same method just change one line
multipartFormData.append(imageData!, withName: "image", fileName:
"image.png", mimeType: "image/png")
it will give me like this
SUCCESS: {
error = "error data post";
}
Please help me!!
There is no such MIME Type as image/jpg. Change it to image/jpeg.
So the error was from the server side, that had a very small size limit for images and that's why they weren't saving. Try to updated the compression for the JPEG images from
if let imageData = UIImageJPEGRepresentation(selectedLogo, 1)
to
if let imageData = UIImageJPEGRepresentation(selectedLogo, 0.6)
Hope this will help you.
I am a newbie to iOS and using Alamofire to upload images. The code I have written is as follows:
let image = imageView.image
let imgData = UIImageJPEGRepresentation(image!, 0.2)!
let headers: HTTPHeaders = [
"x-access-token": "######",
"Accept": "application/json"
]
let parameters = ["profile_picture": "kinza"]
let url = try! URLRequest(url: URL(string:"######")!, method: .post, headers: headers)
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(imgData, withName: "profile_picture",fileName: "kinza.jpg", mimeType: "image/jpg")
for (key, value) in parameters {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
},
with: url)
{ (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)
}
}
When I run this code I get the following log:
2017-11-07 11:03:21.595826+0500 TestApiProject[2457:51089] [] nw_socket_get_input_frames recvmsg(fd 6, 4096 bytes): [54] Connection reset by peer
2017-11-07 11:03:21.596094+0500 TestApiProject[2457:51089] TIC Read Status [1:0x6040003612c0]: 1:54
2017-11-07 11:03:21.596495+0500 TestApiProject[2457:51089] [] nw_socket_output_finished shutdown(6, SHUT_WR): [57] Socket is not connected
2017-11-07 11:03:21.597203+0500 TestApiProject[2457:51089] Task <9A25E63E-EC42-419C-A0B7-02998177EDCA>.<1> HTTP load failed (error code: -1005 [4:-4])
2017-11-07 11:03:21.597681+0500 TestApiProject[2457:51091] Task <9A25E63E-EC42-419C-A0B7-02998177EDCA>.<1> finished with error - code: -1005
Upload Progress: 1.0
nil
It have searched but didn't find anything specific to 'error code -1005' How can i solve it? Did i miss anything?
I have certain questions regarding it:
If http load failed then how did control go to success block of
Alamofire request?
The value of response.result.value is nil. Is something wrong on my
end or on server side?
Do I need to add some more attributes to my info.plist? This is how
it is currently.
Any help would be much appreciated.
Try This..
let image = imageView.image
let imgData = UIImageJPEGRepresentation(image, 0.2)!
let parameters = ["profile_picture": "kinza"]
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(imgData, withName: "profile_picture",fileName: "kinza.jpg", mimeType: "image/jpg")
for (key, value) in parameters {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
},to:"http://YOUR URL"){ (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)
}
}
It's Works For Me..
You can use this method as it worked for me:
fileprivate func p_uploadImage(_ image: UIImage) {
let parameters = ["channelName" : "Your text param"]
let fileData = UIImageJPEGRepresentation(image, 0.2)
let URL2 = try! URLRequest(url: "Your URL", method: .post, headers: ["Authorization" : authKey!])
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(fileData!, withName: "upfile", fileName: "upfile", mimeType: "text/plain")
for (key, value) in parameters {
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
}, with: URL2 , encodingCompletion: { (result) in
switch result {
case .success(let upload, _, _):
print("s")
upload.responseJSON {
response in
if let JSON = response.result.value as? [String : Any]{
let messageString = JSON["message"] as? String
}else {
let alertError = UIAlertController(title: "Alert", message: "Image upload error", preferredStyle: UIAlertControllerStyle.alert)
alertError.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
self.present(alertError, animated: true, completion: nil)
}
}
}
case .failure(let encodingError):
print(encodingError)
let alertError = UIAlertController(title: "Alert", message: "Image upload error", preferredStyle: UIAlertControllerStyle.alert)
alertError.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
self.present(alertError, animated: true, completion: nil)
}
}
)
}
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 = "http://myapp.com/a/images_upload"
let headers = [ "Content-Type":"application/x-www-form-urlencoded"]
let imageData: Data = (UIImageJPEGRepresentation(img, 0.6) as Data?)!
print(imageData)
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
print(parameters)
multipartFormData.append(imageData as Data, withName: "home-\(self.index)", fileName: "home-\(self.index)", mimeType: "image/jpeg")
for (key, value) in parameters {
print(key,value)
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):
print(encodingError)
break
}}}}
Hope somebody can help...Thanks...:)
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{
print(response.result.error!.localizedDescription)
return
}