使用loopj android将图像上载到基于slim框架的REST api?

使用loopj android将图像上载到基于slim框架的REST api?,android,image-uploading,slim,Android,Image Uploading,Slim,我正在尝试使用Params上传图像 Android代码: 将数据发布到服务器 RequestParams params = new RequestParams(); params.put("item_name", "Name of item"); params.put("item_image", encodedImage); MyRestClient.post(MainActivity.this, "item",para

我正在尝试使用Params上传图像

Android代码:

将数据发布到服务器

 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;

        }
Slim框架代码:

    $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);
});
问题:

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


我做的事情是正确的还是遗漏了什么?

谢谢。我只是想告诉大家,图片将与其他字段一起发布。它是一个带有图像选项的窗体。在这种情况下,我会写$request->getUploadedFiles()?要访问其他请求参数,可以使用
$request->geteParams()
(如访问
$\u POST
)。将
$request->getUploadedFiles()
视为访问
$\u文件
。我想在Android代码(客户端post代码)中的上述代码成功或失败后更新我的UI吗?我可以保留一个标志变量吗?或者有其他方法吗?谢谢。我还有一个问题。我将该项存储为sqlite中的json字符串。我想在服务器上发布这个JSON。现在为了发布,我将把JSON转换为对象。然后将其列在列表中。问题是,我的项目的每个对象都包含我希望在发布到服务器时上载的图像。如何实现此功能?我试过了,但找不到办法。在我看来,一种方法是存储图像的本地路径,然后发出多个POST请求(每条记录一个请求)来发送数据。对不起,我并没有从评论中真正了解到这一点。你能把它作为一个新问题发布吗?谢谢。我只是想告诉大家,图片将与其他字段一起发布。它是一个带有图像选项的窗体。在这种情况下,我会写$request->getUploadedFiles()?要访问其他请求参数,可以使用
$request->geteParams()
(如访问
$\u POST
)。将
$request->getUploadedFiles()
视为访问
$\u文件
。我想在Android代码(客户端post代码)中的上述代码成功或失败后更新我的UI吗?我可以保留一个标志变量吗?或者有其他方法吗?谢谢。我还有一个问题。我将该项存储为sqlite中的json字符串。我想在服务器上发布这个JSON。现在为了发布,我将把JSON转换为对象。然后将其列在列表中。问题是,我的项目的每个对象都包含我希望在发布到服务器时上载的图像。如何实现此功能?我试过了,但找不到办法。在我看来,一种方法是存储图像的本地路径,然后发出多个POST请求(每条记录一个请求)来发送数据。对不起,我并没有从评论中真正了解到这一点。你能把它作为一个新问题发布吗?
<?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');

    }

});