Pregunta

I want to use the Plugin with CakePHP 2.1 as behaviour through $actAs = 'Uploader.Attachment'. While this works fine for static Uploads, I dont know how to use it for ajaxUploads.

The simple Question: Is there anyway to make an ajax-Upload happen through the actAs-Behaviour?

The explanation (if needed): The attachment Behaviour doesn't seem to have any defaults nor any other code regarding ajax (especially not the ajaxField in the following code). Though it works if I include the Plugin via

public function beforeFilter() {
    $this->Uploader = new Uploader(array(
        'ajaxField' => 'qqfile'
    ));
}

So I can use something like:

$data = $this->Uploader->upload($this->Uploader->ajaxField, array('overwrite' => true));

But not with:

$this->request->data;

This is not quite what I was seaching for, as I 1. Still need the actAs-Behaviour for other actions. 2. Have two different inclusions of the plugin, which I have to check that they do the same. 3. Can't automatically create thumbs and attach them to the same entry of the model.

¿Fue útil?

Solución 2

This is what I did now for my specific case. Maybe it helps someone. Not beautiful but it works:

public function beforeFilter() {
    $this->Uploader = new Uploader(array(
        'ajaxField' => 'qqfile'
    ));
}

public function ajax() {        
    $this->set('title_for_layout', 'Upload: AJAX File Upload');
    $this->render('ajax');
}

public function ajax_upload($album_id = null, $album_title = null) {
    $this->set('album_id', $album_id);
    $this->set('album_title', $album_title);
    $album_id = 2;
    $upload_dir = "uploads/$album_id/";

    $this->autoLayout = $this->autoRender = false;
    $this->Uploader->setup(array('uploadDir' => "$upload_dir"));
    $data = $this->Uploader->upload($this->Uploader->ajaxField, array(
        'overwrite' => true,
        'name' => 'uploaderFilename'
    ));
    //Creating a thumb
    $thumb_name = $data['custom_name']."_thumb.".$data['ext'];
    $this->Uploader->resize(array('width' => 250, 'expand' => true, 'aspect' => true, 'append' => '_thumb'));
    if (!empty($data)) {
        $this->Picture->set($data);   
        if ($this->Picture->validates()) {
            // Upload successful
            $data["album_id"]= $album_id;
            $data['path_thumb'] = $upload_dir.$thumb_name;
            $this->Picture->save($data);
            header('Content-Type: application/json');
            echo json_encode(array('success' => true, 'data' => $data));                                
        }
    }
}

Otros consejos

I've answered a couple of your related questions already - I really don't want to go through the plugin's configuration settings (contact the developer himself for specific questions) so I'm just going to assume they are correct since you say it works for static file submission.

  1. $actAs denotes Behavior paramaters; they are defined on Model classes, which by definition should never communicate directly with a view anyway. You can still have your model actAs, if you want, you just can't use a model to manipulate a view's behavior.

  2. This is an AJAX-driven file upload, right? So you probably need to write a simple client-side script to go with the form that lives in your controller action's associated view's to submit the form data to a cake controller method asynchronyously. -- See: http://www.phpletter.com/Demo/AjaxFileUpload-Demo/

  3. Adding the plugin just allows the plugin to handle the Model for you, right? Are you sure you need 2 invocations of the plugin? Or 2 controller actions or 2 controllers or 2 models or something that both use the plugin?

  4. The view will return the data to the controller action, which is probably where you want to send it off to the model for handling. beforeFilter() is one of the controller callback methods; specifically, the one that fires just before any / every method in the controller (see http://book.cakephp.org/2.0/en/controllers.html and also http://book.cakephp.org/2.0/en/views/json-and-xml-views.html for setting up your controller to handle async requests...)

  5. Is thumbnail generation supposed to tax place on the frontend or the backend? >_> If it's the latter, do you need to add the code in your Model that generates the thumbnail from the data? Cake's magic model callbacks should make that pretty straightforward, too: http://book.cakephp.org/2.0/en/models/callback-methods.html

Trying it this way should at least simplify things for you.

HTH

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top