0

Here cakephp image upload working fine without ajax.After using ajax, data is inserted without image directory.

Here is my model code

public function processCoverUpload($check = array()) {
            if (!is_uploaded_file($check['product_image']['tmp_name'])) {
                return FALSE;
            }
            if (!move_uploaded_file($check['product_image']['tmp_name'], WWW_ROOT . 'img' . DS . 'uploads' . DS . $check['product_image']['name'])) {
                return FALSE;
            }
            $this->data[$this->alias]['product_image'] = 'uploads/'. $check['product_image']['name'];
            return TRUE;
        }

Here the controller

if ($this->request->is('post')) {
            $this->MadProduct->create();

            $data = $this->request->data['MadProduct'];
                        if (!$data['product_image']['name']) {
                            unset($data['product_image']);
                        }
            if ($this->MadProduct->save($data)) {
                $this->Session->setFlash(__('The mad product has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The mad product could not be saved. Please, try again.'));
            }
        }

Here is the view file field

echo $this->Form->input('product_image',array('type' => 'file','label' => false,));

Here is the js submission button

 <?php  echo $this->Js->submit('Submit',array(
        'before'=>$this->Js->get('#sending')->effect('fadeIn',array('speed' => 'slow')),
        'success'=>$this->Js->get('#sending')->effect('fadeOut',array('speed' => 'slow')),
         'class' => 'btn btn-danger',
         'style'=>'width:45%;margin-top:1%;height:30px;')
         );   
  ?>

How I will send image directory by jshelper ?

Sagar Guhe
  • 1,041
  • 1
  • 11
  • 35

1 Answers1

0

The JS helper doesn't support file uploads, which would require making use of the "new" XHR2 features.

You could extend the JS helper/engine and add support for files, but since the JS helper is deprecated anyways, I'd suggest to simply make use of JavaScript directly. There are more than enough examples out there showing how to upload files via AJAX.

How can I upload files asynchronously?

Community
  • 1
  • 1
ndm
  • 59,784
  • 9
  • 71
  • 110