CakePHP Uploader 插件 - 行为设置

3
我在使用这个插件:MileJ CakePHP Uploader,它的表现非常好,但是我只能通过控制器使其正常工作,而不能作为模型中的行为。我需要让它作为模型中的行为工作,这样我才能将文件传递到 Amazon S3 中。
以下是我的代码,有人能看出我错在哪里吗?目前,仅生成了数据库记录,而且只有表单中我拥有的其他字段(标题、卡片 ID、用户 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'));?>
1个回答

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

使用这个行为时,你必须在Uploader.Attachment数组中指定你的文件字段的名称。
你的表单字段名为file1,但是这个行为目前正在寻找file

我在开始使用这个插件时也犯了同样的错误!我认为文档有些地方略微不够清晰,这是我的个人看法。不过它是一个很好的插件。 - Ross

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接