2018-02-24 13:41:58 3365次浏览 1条回答 0 悬赏 10 金钱

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>&nbsp;&nbsp;<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));
            } 
        } 
    } 
} 

}

请问各位前辈:为何点上传按钮以后没有任何反应,不执行动作呢?

  • 回答于 2018-02-26 13:19 举报

    给个思路:

    1. 打开chrome 看下html结构是否包含了如下结构,其中button 需要type=submit
      <form> <button></from>
    2. 打开Net 看点击后是否刷新, 一单有刷新 到Controller 里试着获取数据然后debug

    好久不写Yii 1代码了,只能给点思路。

    2 条回复
    回复于 2018-02-27 15:38 回复

    aaaaaaaa

    回复于 2018-02-27 15:38 回复

    adsfdsafdsaf

您需要登录后才可以回答。登录 | 立即注册
breakwind
试用期

breakwind

注册时间:2018-01-23
最后登录:2020-03-27
在线时长:2小时7分
  • 粉丝0
  • 金钱10
  • 威望0
  • 积分30

热门问题