我希望能够将文件上传到数据库,然后有一个查看链接,这样我也可以看到这个文件。基本上所有保存的文件都应该显示在索引页面中,如果我单击查看按钮,文件的内容应该显示在查看页面中。我正在使用这些代码,这是我的控制器部分:
public function add(){
if ($this->request->is('post')){
$this->Doc->create();
$filename = $this->request->data['Doc']['docname']['name'];
$this->request->data['Doc']['docname'] = $filename;
if ($this->Doc->save($this->request->data)){
$this->Session->setFlash('Message : ' . $filename);
$this->redirect(array('action' => 'index'));
}else{
$this->Session->setFlash(__('The Doc could not be saved. Please, try again'));
$this->redirect(array('action' => 'add'));
}
}
}
这是我在 app/view 下的添加功能:
<div class="doc form">
<?php echo $this->Form->create('Doc', array('type' => 'file')); ?>
<fieldset>
<table align="left">
<tr>
<td>
<?php echo $this->Form->input('docname', array('type' => 'file'));?>
</td>
</tr>
</table>
</fieldset>
<div align="center" style="padding: 20px">
<?php echo $this->Html->link('Cancel',array('controller' => 'docs', 'action' => 'index'), array('class' => 'btn btn-primary')); ?>
<?php echo $this->Form->button('Submit',array('type'=>'upload','class'=>'btn btn-primary')); ?>
<?php echo $this->Form->end(); ?>
我得到的只是保存在数据库中的文件名,所有这些名称也显示在我的索引页面中,但是如果我单击查看,我只会看到文件名而不是文件下的内容。我是 cake-php 的新手,所以我怀疑这是否是正确的方法。我正在使用 Cake-Php 2.4 版本。数据库是postgres。我的桌子是这样的:
列类型
id (integer)
docname (character varying(255))
grant_id (integer(fk))
我的 Doc 模型看起来像这样
class Doc extends AppModel {
//The Associations below have been created with all possible keys, those that are not needed can be removed
public $belongsTo = array(
'Grant' => array(
'className' => 'Grant',
'foreignKey' => 'grant_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}