简体   繁体   中英

file upload in yii?

I am newbie to yii. I have a project in which i have to send message to users and upload a file of 1 MB to them. Its all database based no Mail function is user. I have sent message to users and according to user id that message is shown to them. All works fine but in attachment i can upload any file like excel, image and pdf. How to show this with message. Please help.

my action

public function actionCreateMessage()
{
    $model=new Message;

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['File']))
    {
        $file=$model->filename=CUploadedFile::getInstance($model,'filename');
        if(empty($file)){
            $model->attributes=$_POST['Message'];
            $model->save();
        }
        else{
        $model->attributes=$_POST['Message'];
        $model->filename = CUploadedFile::getInstance($model, 'filename');
        if($model->save()){
            $simpan->saveAs(Yii::app()->basePath .
                '/../files/' . $model->filename.'');
            $this->redirect(array('view','id'=>$model->message_id));
        }
        }
    }

    $this->render('create',array(
        'model'=>$model,
    ));
}


viewmessage

<?php $this->widget('bootstrap.widgets.TbDetailView',array(
       'data'=>$model,
       'attributes'=>array(
                      'message_id',
                      'message_subject',
                      'message_body',
                       array(  
            'label'=>'attachment',
            'type'=>'raw',
            'value'=>CHtml::link(CHtml::encode($model->filename);

        ),
                              ),
 )); ?>

i want filename linkable and on click image/pdf/excel should opens

First of all if you're just getting started with Yii I strongly recommend going with Yii2 instead of Yii1.x. That code you posted is Yii 1.x and right now its on life-support plus Yii2 is far more elegant.

But back to business:

Out of the box there isn't a Yii specific way for image and Excel files to just display. Some if not all browsers have built-in support for PDFs so those will display fine in most browsers.

But if you want a all-in-one solution that allows the user to click on a link and have the document open you will probably need to try a few things:

  1. Upload the Excel doc to a Google Docs account and embed it in your page by following these directions: https://support.google.com/docs/answer/143345?hl=en&rd=1

  2. For images you can just create a tag and it will display.

  3. For PDFs you could try something like this:

<div> <object data="test.pdf" type="application/pdf" width="300" height="200"> alt : <a href="test.pdf">test.pdf</a> </object> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM