cakephp web services how to write an action that allows me to upload a file cakephp web services how to write an action that allows me to upload a file json json

cakephp web services how to write an action that allows me to upload a file


I figured it out after sometime.

Assuming the following setup

  • Cakephp 2.x
  • the action here is public for anonymous users

Step 1. Install Webservice Plugin by josegonzalez.

Step 1.1. Setup Router::parseExtensions for json

Step 1.2. Add 'Webservice.Webservice' to the components of PostController

Step 1.3. Load the Plugin


Step 2. You need to change the following action for PostController

    public function add() {         if ($this->request->is('post')) {        // create new Post -- this will grab the file from the request data        $newPost = $this->Post->createNew($this->request->data);            if ($newPost) {            $this->Session->setFlash(__('Your Post has been saved'));            // for normal webpage submission            if (empty($this->request->params['ext'])) {                $this->redirect('/');                               } else {                        // for json response to Flex client                 $result = $newPost;                $error = null;                $id = null;            }        } else {            $this->Session->setFlash(__('Your Post could not be saved. Please, try again.'));            // for json response for failure to create            if (!empty($this->request->params['ext'])) {                                                    $result = null;                    $error = 'Your Post could not be saved.';                    $id = null;                        }                    }        // this is for json response via Webservice.Webservice        $this->set(compact('result', 'error', 'id'));            }    }

Step 3. Setup your Flex code as stated in this answer here. This is how you then retrieve the JSON response in Flex Actionscript.


Step 4. You should expect to get back a json response consisting of the 3 variables result, error, and id and the cake validationErrors. You may choose to blacklist validationErrors as stated in the plugin.


It sounds like your Cakephp app is more or less set up appropriately, but that comes later. Your challenge now is to get your Adobe Flex code to POST the required data to the Cakephp action url. I see you're using an admin action so I'm assuming a user login is required to access that action. I know MINISCULE flex code, but What you'll need to do in Flex is something like this:

  1. Have Flex POST to your /user/login page using the username/pw combo that has admin access and save the response cookies and session info into a variable. Using something like this:

    var request:URLRequest = new URLRequest(url);request.method = "POST";request.data = variables; //variables includes username and pwtry {               navigateToURL(request);}catch (e:Error) {// handle error here}
  2. Then have Flex POST (include cookies in request header) to your admin/posts/action with the image data as the body (content) of the request.

All you then have to do in the cakephp action is to take $this->data['body'] and save it into a file.


On your website you could make a normal form that uploads text and images etc.

Then where ever you want to upload an image from, you would just send the correct POST information.

For example if you wanted to use your service from another server altogether you could use CURL and send the form data using code like this:

$ch = curl_init();curl_setopt($ch, CURLOPT_POST, true);curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => '@/folder/image.jpg'));curl_setopt($ch, CURLOPT_URL, 'http://website.com/posts/add');curl_exec($ch);curl_close($ch);

I'm not sure if Flex supports CURL but it should have something similar.