使用 loopj android 将图像上传到基于 REST 的 slim framework api?

Upload image with loopj android to slim framework based REST api?

我正在尝试使用参数上传图片。

Android代码:

POST 数据到服务器

 RequestParams params = new RequestParams();
            params.put("item_name", "Name of item");
            params.put("item_image", encodedImage);

            MyRestClient.post(MainActivity.this, "item",params, new JsonHttpResponseHandler(){

                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONArray response) {

                    JSONArray jArr = response;

                    super.onSuccess(statusCode, headers, response);
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

                    String responseFromAPI = responseString;

                    super.onFailure(statusCode, headers, responseString, throwable);
                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString) {

                    String responseStr = responseString;

                    super.onSuccess(statusCode, headers, responseString);
                }


                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

                    JSONObject jObj = response;

                    super.onSuccess(statusCode, headers, response);
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {

                    JSONObject jOBj = errorResponse;

                    super.onFailure(statusCode, headers, throwable, errorResponse);
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
                    JSONArray jArr = errorResponse;

                    super.onFailure(statusCode, headers, throwable, errorResponse);
                }
            });

位图图像编码

  public String getStringImage(Bitmap bmp){

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] imageBytes = baos.toByteArray();
            String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);

            return encodedImage;

        }

超薄框架代码:

    $app->post('/image', function ($request, $response) {


    $input          =   $request->getParsedBody();

    $uploaded_image =   $input['image_image'];    

    $path           =   "/..../uploads/"."img-".date("Y-m-d-H-m-s").".jpg";

    if (file_put_contents($path, base64_decode($uploaded_image)) != false)
    {

        $sql = "INSERT INTO item (item_name, item_image) VALUES (:restaurant_name, :restaurant_image)";

        $sth = $this->db->prepare($sql);
        $sth->bindParam("item_name", $input['item_name']);    
        $sth->bindParam("item_image", $input['item_image']); 

        $sth->execute();

        $input['id'] = $this->db->lastInsertId();

    }


    return $this->response->withJson($input);
});

问题:

照片应该按照代码和我的理解上传。它没有将图像上传到所需的文件夹。

我做的是正确的还是我错过了什么?

<?php
$app->post('/image', function ($request, $response) {

    $files = $request->getUploadedFiles();
    $file = $files['image_image']; // uploaded file

    $parameters = $request->getParams(); // Other POST params

    $path = "/..../uploads/"."img-".date("Y-m-d-H-m-s").".jpg";

    if ($file->getError() === UPLOAD_ERR_OK) {

        $file->moveTo($path); // Save file

        // DB interactions here...

        $sql = "INSERT INTO item (item_name, item_image) VALUES (:restaurant_name, :restaurant_image)";

        $sth = $this->db->prepare($sql);
        $sth->bindParam("item_name", $input['item_name']);    
        $sth->bindParam("item_image", $input['item_image']); 

        // if statement is executed successfully, return id of the last inserted restaraunt
        if ($sth->execute()) {

            return $response->withJson($this->db->lastInsertId());

        } else {

            // else throw exception - Slim will return 500 error
            throw new \Exception('Failed to persist restaraunt');

        }

    } else {

        throw new \Exception('File upload error');

    }

});