I am trying to upload file to server. I am using SlimFramework and PHP in my backend code and it is in RESTful form. Image uploading works very well if I upload it through Postman however I am not able to get it to work using Swift and Alamofire library.
In the route I have in backend, I get the file like this, from body parameter:
$image = $request->getUploadedFiles();
and to upload I use the:
$image->moveTo("myPath");
In Swift I have router, which gives .post
method and the route URL and I tried like this:
func uploadImage(user_id: Int, image: Data, completion: @escaping (_ error: Error?, _ success: Bool)->Void) {
let parameters = [
"user_id": user_id,
"newFile": image
] as [String : Any]
Alamofire.request(Router.imageUplaod(parameters: parameters))
.validate(statusCode: 200..<300)
.responseJSON { response in
switch response.result{
case .failure(let error):
completion(error, false)
print(error)
case .success(let value):
//Registered sucesfully! let json = JSON(value)
completion(nil, true)
}//Switch case
}//Alamofire
}
And I call it like this:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let chosenProfileImage = info[UIImagePickerControllerEditedImage] as! UIImage
let imageData = UIImageJPEGRepresentation(chosenProfileImage, 0.2)
profileImageView.image = chosenProfileImage
dismiss(animated: true) {
API.uploadImage(user_id: User.sharedInstance.user_id, image: imageData!) { (error, success) in
if success{
print("IMAGE UPLOADDED")
}
}
}
}
This is my backend route:
$this->post('/image/upload', function($request, $response, $args){
global $database;
$data = $request->getParsedBody();
$user_id = $data['user_id'];
$files = $request->getUploadedFiles();
if (empty($files['newfile'])) {
throw new Exception('Expected a newfile');
}
$newfile = $files['newfile'];
if ($newfile->getError() === UPLOAD_ERR_OK) {
$uploadFileName = "profileImage".$user_id.".jpg";
$newfile->moveTo("/path/to/image/$uploadFileName");
}
$database->query("UPDATE `users` SET `profile_image_url` = :image WHERE user_id = :user_id");
$database->bind(':image', $uploadFileName);
$database->bind(':user_id', $user_id);
$database->execute();
$Jsonarray['status'] = "true";
$Jsonarray['message'] = "Image Uploaded Successfully";
return $response->withStatus(200)->withHeader('Content-Type', 'application/json')->write(json_encode($Jsonarray));
});
Weird thing is that Alamofire outputs status code 500 but the server do not tell me nothing about it. The problem must be in the Swift part. So I do not know even how to debug it.
How should I solve the problem?
I solved the problem. I rebuilt it that way:
So in Swift I Convert it like this:
let chosenProfileImage = info[UIImagePickerControllerEditedImage] as! UIImage
let imageData = UIImageJPEGRepresentation(chosenProfileImage, 0.2)
profileImageView.image = chosenProfileImage
let base64String = imageData?.base64EncodedString(options: .lineLength64Characters)
And backend is pretty much same, just converted it back to file using base64_decode();
and used file_put_contents();
function to move it to server.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With