Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting image file into body parameter

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?

like image 563
Tarvo Mäesepp Avatar asked May 03 '17 22:05

Tarvo Mäesepp


1 Answers

I solved the problem. I rebuilt it that way:

  1. Convert image to Base64 String on client
  2. Decode it to image on backend
  3. Upload it

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.

like image 66
Tarvo Mäesepp Avatar answered Oct 04 '22 10:10

Tarvo Mäesepp