yii1.1的文件上传操作,为何点上传按钮以后没有任何反应,不执行动作呢? [ 1.1 版本 ]
view:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'add-form',
'enableClientValidation'=>true,
'clientOptions'=>array( 'validateOnSubmit'=>true,),
'htmlOptions'=>array('enctype'=>'multipart/form-data'),
));
?>
<table>
<tr>
<td width="20%">
<b>上传文件:</b> <font color="red">*</font>
</td>
<td width="80%">
<?php echo CHtml::activeFileField($model, 'file'); ?>
<?php echo $form->error($model,'file');?>
</td>
</tr>
</table>
<table>
<tr>
<td>
<?php echo CHtml::button('上传', array('submit' => array('file/index'))); ?>
</td>
</tr>
</table>
<?php $this->endWidget() ?>
model:
<?php
/**
* LoginForm class.
*/
class FileForm extends CFormModel
{
/**
*/
public $file;
public $types;
public $maxSize;
public $tooLarge;
public function rules()
{
return array(
array('file',
'file',
'allowEmpty'=>true ,
'types'=>'jpg, jpeg, gif, png, doc, , docx, txt, dwg, xls, xlsx, ppt, pptx, avi, mpg, rar, zip, 7z, exe, ini, dll, pdf',
'maxSize'=>1024 * 1024 * 10, // 10MB
'tooLarge'=>'The file was larger than 10MB. Please upload a smaller file.',
),
);
}
}
controller:
<?php
class FileController extends Controller
{
public function actionIndex()
{
$fileModel = new FileForm;
$this->render('index',array('model'=>$fileModel));
}
// public function actionFilepath()
// {
// $filePath = Yii::app()->request->baseUrl.'/filestore/'.Yii::app()->user->name
// }
public function actionUpload(){
$fileModel = new FileForm();
if(isset($_POST["Files"])){
$fileModel->attributes=$_POST['Files'];
$file = CUploadedFile :: getInstance($fileModel, 'file');
if(is_null($file)){
yii::app ()->user->setFlash('failed', '请选择上传文件');
$this->render('index', array('model' => $fileModel));
return ;
}
if (is_object($file) && get_class($file) == 'CUploadedFile') {
Yii::log("File Name : " . $file->getName() );
// 文件类型
$fileModel->fileType = strtolower($file->getExtensionName());
// 存储文件名
$newFileName = date('YmdHis') . '_' . rand(1000, 9999) . '.' . $fileModel->fileType;
// 服务器端存储路径
// $newFilepath = Yii::app()->params['upload_folder'] . $newFileName;
$newFilepath = Yii::app()->request->baseUrl.'/filestore/'.Yii::app()->user->name.'/'.$newFileName;
// 上传文件名
$fileModel->fileName = $file->getName();
// 文件类型 (application/x-msdownload、application/pdf、application/octet-stream)
$fileModel->fileType = $file->getType();
// 文件大小
$fileModel->fileSize = $file->getSize();
if ($fileModel->validate() && $fileModel->save()){
// 将文件存在在服务器端
$file->saveAs($newFilepath);
yii::app ()->user->setFlash('successed', '上传成功');
} else {
yii::app ()->user->setFlash('failed', '上传失败');
}
$this->render('index', array('model' => $model));
}
else{
$this->render('index', array( 'model' => $model,
));
}
}
}
public function actionDownload(){
if (isset($_GET["id"])) {
$id = $_GET["id"];
$fileModel = FileForm::model()->find('id =:id', array('id' => $id));
if ($fileModel == null) {
throw new CHttpException ('500', '文件不存在');
} else {
// 服务器端文件的路径
$fileName = $fileModel->saveFilePath ;
if (file_exists($fileName)){
yii::app ()->request->sendFile ($fileModel->fileName, file_get_contents ($fileName));
}
}
}
}
}
请问各位前辈:为何点上传按钮以后没有任何反应,不执行动作呢?
共 1 个回答
breakwind
注册时间:2018-01-23
最后登录:2020-03-27
在线时长:2小时7分
最后登录:2020-03-27
在线时长:2小时7分
- 粉丝0
- 金钱10
- 威望0
- 积分30