zoukankan      html  css  js  c++  java
  • yii 上传下载


    页面
    -------------------------------------------------
    Html代码 复制代码 收藏代码
    1.   
    2. <?php $form=$this->beginWidget('CActiveForm', array(   
    3.         'id'=>'add-form',   
    4.         'enableClientValidation'=>true,   
    5.         'clientOptions'=>array( 'validateOnSubmit'=>true,),   
    6.         'htmlOptions'=>array('enctype'=>'multipart/form-data'),     
    7.     ));    
    8. ?>  
    9.     <table>  
    10.         <tr>  
    11.             <td width="20%">  
    12.                 <b>ファイルパス:</b>&nbsp;&nbsp;<font color="red">*</font>  
    13.             </td>  
    14.             <td width="80%">  
    15.                 <?php echo CHtml::activeFileField($model, 'file'); ?>  
    16.                 <?php echo $form->error($model,'file');?>  
    17.             </td>  
    18.         </tr>  
    19.     </table>  
    20.   
    21.     <table>  
    22.         <tr>  
    23.             <td>  
    24.                 <?php echo CHtml::button('上传', array('submit' => array('downfiles/upload'))); ?>  
    25.             </td>  
    26.         </tr>  
    27.     </table>  
    <?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('downfiles/upload'))); ?>
                </td>
            </tr>
        </table>
    



    Model
    -------------------------------------------------
    Php代码 复制代码 收藏代码
    1. public function rules()   
    2. {   
    3.     return array(   
    4.                    
    5.         array('file''file','allowEmpty'=>true ,   
    6.                 'types'=>'jpg, gif, png, doc, txt',   
    7.                 'maxSize'=>1024 * 1024 * 10, // 10MB   
    8.                 'tooLarge'=>'The file was larger than 10MB. Please upload a smaller file.',   
    9.             ),   
    10.     );   
    11. }  
    public function rules()
    {
    	return array(
                    
            array('file', 'file','allowEmpty'=>true ,
                    'types'=>'jpg, gif, png, doc, txt',
                    'maxSize'=>1024 * 1024 * 10, // 10MB
                    'tooLarge'=>'The file was larger than 10MB. Please upload a smaller file.',
                ),
    	);
    }
    


    上传
    -------------------------------------------------
    Php代码 复制代码 收藏代码
    1. public function actionUpload(){   
    2.        
    3.     $model = new DownFiles();   
    4.        
    5.     if(isset($_POST["DownFiles"])){   
    6.            
    7.         $model->attributes=$_POST['DownFiles'];   
    8.   
    9.         $file = CUploadedFile :: getInstance($model'file');   
    10.            
    11.         if(is_null($file)){   
    12.             yii::app ()->user->setFlash('failed''请选择上传文件');   
    13.             $this->render('upload'array('model' => $model));   
    14.             return ;   
    15.         }   
    16.            
    17.         if (is_object($file) && get_class($file) == 'CUploadedFile') {   
    18.                
    19.             Yii::log("File Name : "  . $file->getName() );   
    20.                
    21.             // 文件类型   
    22.             $model->fileType = strtolower($file->getExtensionName());   
    23.   
    24.             // 存储文件名   
    25.             $newFileName = date('YmdHis') . '_' . rand(1000, 9999) . '.' . $model->fileType;    
    26.             // 服务器端存储路径   
    27.             $newFilepath = Yii::app()->params['upload_folder'] . $newFileName;   
    28.   
    29.             // 上传文件名   
    30.             $model->fileName = $file->getName();   
    31.             // 文件类型 (application/x-msdownload、application/pdf、application/octet-stream)   
    32.             $model->fileType = $file->getType();   
    33.             // 文件大小   
    34.             $model->fileSize = $file->getSize();   
    35.                
    36.             if ($model->validate()  && $model->save()){   
    37.                 // 将文件存在在服务器端   
    38.                 $file->saveAs($newFilepath);   
    39.   
    40.                 yii::app ()->user->setFlash('successed''上传成功');   
    41.             } else {   
    42.                 yii::app ()->user->setFlash('failed''上传失败');   
    43.             }   
    44.                
    45.         } else {   
    46.             yii::app ()->user->setFlash('failed''上传失败');   
    47.         }   
    48.            
    49.         $this->render('upload'array('model' => $model));   
    50.            
    51.     }else{   
    52.         $this->render('upload'array(   
    53.             'model' => $model,   
    54.         ));   
    55.     }   
    56.        
    57. }  
    public function actionUpload(){
        
        $model = new DownFiles();
        
        if(isset($_POST["DownFiles"])){
            
            $model->attributes=$_POST['DownFiles'];
    
            $file = CUploadedFile :: getInstance($model, 'file');
            
            if(is_null($file)){
                yii::app ()->user->setFlash('failed', '请选择上传文件');
                $this->render('upload', array('model' => $model));
                return ;
            }
            
            if (is_object($file) && get_class($file) == 'CUploadedFile') {
                
                Yii::log("File Name : "  . $file->getName() );
                
                // 文件类型
                $model->fileType = strtolower($file->getExtensionName());
    
                // 存储文件名
                $newFileName = date('YmdHis') . '_' . rand(1000, 9999) . '.' . $model->fileType; 
                // 服务器端存储路径
                $newFilepath = Yii::app()->params['upload_folder'] . $newFileName;
    
                // 上传文件名
                $model->fileName = $file->getName();
                // 文件类型 (application/x-msdownload、application/pdf、application/octet-stream)
                $model->fileType = $file->getType();
                // 文件大小
                $model->fileSize = $file->getSize();
                
                if ($model->validate()  && $model->save()){
                    // 将文件存在在服务器端
                    $file->saveAs($newFilepath);
    
                    yii::app ()->user->setFlash('successed', '上传成功');
                } else {
                    yii::app ()->user->setFlash('failed', '上传失败');
                }
                
            } else {
                yii::app ()->user->setFlash('failed', '上传失败');
            }
            
            $this->render('upload', array('model' => $model));
            
        }else{
            $this->render('upload', array(
                'model' => $model,
            ));
        }
        
    }
    
    





    文件
    -------------------------------------------------
    Java代码 复制代码 收藏代码
    1.   
    2. public function actionDownload(){   
    3.        
    4.     if (isset($_GET["id"])) {   
    5.         $id = $_GET["id"];   
    6.   
    7.         $model = DownFiles::model()->find('id =:id', array('id' => $id));   
    8.            
    9.         if ($model == null) {   
    10.             throw new CHttpException ('500''文件不存在');   
    11.         } else {   
    12.             // 服务器端文件的路径   
    13.             $fileName = $model->saveFilePath ;   
    14.                
    15.             if (file_exists($fileName)){   
    16.                 yii::app ()->request->sendFile ($model->fileName,  file_get_contents ($fileName));   
    17.             }   
    18.         }   
    19.     }   
    20. }  
  • 相关阅读:
    DOS命令行编译运行java
    mysql安装
    ICCV2021 | Vision Transformer中相对位置编码的反思与改进
    ICCV2021 | 医学影像等小数据集的非自然图像领域能否用transformer?
    ICCV2021 | TransFER:使用Transformer学习关系感知的面部表情表征
    2021视频监控中的多目标跟踪综述
    ML2021 | (腾讯)PatrickStar:通过基于块的内存管理实现预训练模型的并行训练
    ICCV2021 | SOTR:使用transformer分割物体
    ICCV2021 | PnPDETR:用Transformer进行高效的视觉分析
    使用Latex/Tex创建自己的简历。
  • 原文地址:https://www.cnblogs.com/xiongsd/p/3090917.html
Copyright © 2011-2022 走看看