3

我正在使用这个插件:MileJ CakePHP Uploader,它工作得非常好,但是我只能让它通过控制器工作,而不是作为我需要工作的模型中的一种行为,所以我可以使用该功能将文件传递到亚马逊s3。

我的代码如下,谁能看到我哪里出错了?目前数据库记录已生成,但仅与我在表单上的其他字段(标题,card_id,user_id)无关,但与文件无关。该文件也没有上传。

模型:DataFile.php

public $actsAs = array(
    'Uploader.FileValidation' => array(
        'file1' => array(
            'required' => true
        ),
        'file2' => array(
            'required' => false
        ),
        'file3' => array(
            'required' => true
        )
    ),
    'Uploader.Attachment' => array(
        'file' => array(
            'name' => '',
            'uploadDir' => 'files/data_files/',
            'dbColumn' => 'path',
            'maxNameLength' => 30,
            'overwrite' => true,
            'stopSave' => false,
            's3'        => array(
                                'accessKey' => 'MYACCESSKEY',
                                'secretKey' => 'MYSECRETKEY',
                                'ssl' => true,
                                'bucket' => 'testfilespath',
                                'path' => '/'
                            ),                  // Array of Amazon S3 settings              
            'metaColumns' => array(
                    'ext' => 'extension',
                    'size' => 'bytesize',
                    'group' => 'group',
                    'width' => 'width',
                    'height' => 'height',
                    'filesize' => 'filesize'
            )
        )
    )
);  

控制器:DataFileController.php

// ADD BY BEHAVIOUR NEW FILE(S) - NOT WORKING
// ---------------------------------------------------------->
function add_behavior() 
{
    if (!empty($this->request->data)) 
    {

        if ($this->DataFile->save($this->request->data)) 
        {
            debug($this->request->data);

            $this->Session->setFlash(__('The File has been uploaded');
            $this->redirect(array('action' => 'index'));
        } 
        else 
        {
            $this->Session->setFlash(__('The DataFile could not be saved. Please, try again.'));
        }
    }
}

查看:add_behavior.ctp

<?php echo $this->Form->create('DataFile', array('type' => 'file')); ?>
    <?php
    echo $this->Form->input('user_id', array('value' => $this->Session->read("Auth.User.id"),  'type' => 'text'));
    echo $this->Form->input('card_id', array('value' => '1',  'type' => 'text'));
    echo $this->Form->input('caption', array('label' => 'File Title'));
    echo $this->Form->input('file1', array('type' => 'file', 'label' => 'File'));
    ?>
    <?php echo $this->Form->end(__('Upload'));?>
4

1 回答 1

2
Uploader.Attachment => array(
        'file1' => array(...),
        'file2' => array(...),
        'file3' => array(...),
);

使用该行为时,您必须在Uploader.Attachment数组中指定文件字段的名称。

你的表单域被调用file1,行为当前正在寻找file

于 2012-10-17T09:07:02.380 回答