Мне нужно загрузить изображение, используя метод Alamofire.upload(multipartFormData
.Мой код указан ниже, но я получаю сообщение об ошибке, подобное этому:
FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error
Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character
0." UserInfo={NSDebugDescription=Invalid value around character 0.}))
Может кто-нибудь сказать мне, как решить проблему?
func photoSave(_ photoImage : UIImage, type : String)
{
let urlString = ServerUrl + "document_upload"
let imageUpload = UIImageJPEGRepresentation(photoImage, 0.5)
Alamofire.upload(multipartFormData: { (multipartFormData) in
let date = Date().timeIntervalSince1970 * 1000
let fileName = String(date) + "Image.jpg"
multipartFormData.append(imageUpload!, withName: "image", fileName: fileName, mimeType: "image/jpg")
multipartFormData.append((self.token.data(using: String.Encoding.utf8, allowLossyConversion: false)!), withName: "token")
multipartFormData.append((type.data(using: String.Encoding.utf8, allowLossyConversion: false))!, withName: "document_type")
}, to: urlString, method: .post, encodingCompletion: { (result) in
switch result {
case .success(let upload, _, _):
upload.responseJSON { response in
print("response11 : ", response.request?.url! as Any,response)
if response.result.isSuccess
{
let result : AnyObject = response.result.value as AnyObject
let success = result["status_message"] as? String
if success == "Image Upload Successfully"
{
DispatchQueue.main.async
{
}
}
else
{
let msg = result["status_message"] as? String
DispatchQueue.main.async
{
let alert = UIAlertController(title: msg, message: "", preferredStyle: .alert)
let ok = UIAlertAction(title: "OK", style: .cancel, handler: { Void in
})
alert.addAction(ok)
self.present(alert, animated: true, completion: nil)
return
}
}
}
else
{
DispatchQueue.main.async
{
let alert = UIAlertController(title: "Network Connection Lost", message: "Please try again", preferredStyle: .alert)
let ok = UIAlertAction(title: "OK", style: .cancel, handler: { Void in
})
alert.addAction(ok)
self.present(alert, animated: true, completion: nil)
}
return
}
}
case .failure(let encodingError):
print(encodingError)
}
})
}