[SOLVED !!] Opening file from database

Hi all,

I am trying to open file from DB using Contoller code from this Wiki.

In my controller I have code:




public function displayCv()

{

	$model=$this->loadModel();

	header('Content-Type: '.$model->cv_file_type);

	echo $model->cv_file_content;  // echo data from blob

}



And in view:




array(              

      'label'=>'CV',

      'name'=>'cv',

      'type'=>'raw',

      'value'=>CHtml::link($model->cv, HOW_TO_CALL_DISPLAY_CV()<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />),

),



I tried to call displayCv function from Contoller ($this->displayCv, etc…) but still getting errors.

Any help would be appreciated.

The function name should start with action, in order to be an action of the controller.

Try:




public function actionDisplayCv()

{

        $model=$this->loadModel();

        header('Content-Type: '.$model->cv_file_type);

        echo $model->cv_file_content;  // echo data from blob

}



And then calling a url generated bt CHtml::link(‘download curriculum’, array(‘displayCv’, ‘id’=>$model->primaryKey));

Where 12 is a valid primary key.

This code:




public function actionDisplayCv()

{

$model=$this->loadModel();

header('Content-Type: '.$model->cv_file_type);

echo $model->cv_file_content;  // echo data from blob

}



outputs:

[size="5"][color="#FF0000"]PHP Error[/color][/size]

Description

Missing argument 1 for CandidateController::loadModel()

What seems to be obvious, because loadModel() takes argument.

When I change code to:




public function actionDisplayCv()

{

/////////////////////   $id as argument /////////

$model=$this->loadModel($id);

header('Content-Type: '.$model->cv_file_type);

echo $model->cv_file_content;  // echo data from blob

}



I get CHttpException on the view:

[size="5"]Error 404[/size]

The requested page does not exist.

That means that this function loadModel():




public function loadModel($id)

{

$model=Candidate::model()->findByPk((int)$id);

if($model===null)

throw new CHttpException(404,'The requested page does not exist.');

return $model;

}



Throws an exception - it looks like ‘id’ is not being pased from view to controller… Any suggestions?

[SOLVED !!]

Sorry for replying to myself, but I had to bump it up.

I got it working. The thing was about [color="#FF0000"]‘id’[/color] not being retrieved by controller.

In the view we call this:




'value'=>CHtml::link($model->cv,array('displayCv','id'=>$model->primaryKey)),



The link looks the like: ht.tp://myserver.pl/yii/myapp/index.php?r=candidate/displayCv&id=1000006

But I had to use controller code:




public function actionDisplayCv()

{ 

	$model=$this->loadModel($_GET['id']);


	header('Pragma: public');

	header('Expires: 0');

	header('Cache-Control: must-revalidate, post-check=0, pre-check=0');

	header('Content-Transfer-Encoding: binary');

	header('Content-length: '.$model->cv_file_size);

	header('Content-Type: '.$model->cv_file_type);

	header('Content-Disposition: attachment; filename='.$model->cv);


	echo $model->cv_file_content;

}



[color="#00FF00"][/color][color="#008000"][/color]

Note: you have to call [color="#008000"]$model=$this->loadModel($_GET[‘id’]);[/color]

Not[color="#FF0000"] $model=$this->loadModel();[/color] - like previously in the Wiki - NOW CORRECTED.

Nor [color="#0000FF"]$model=$this->loadModel($id);[/color]

Hope it [color="#FF0000"]h[/color][color="#006400"]e[/color][color="#0000FF"]l[/color][color="#FFA500"]p[/color][color="#9932CC"]s[/color] someone.

Now [color="#FF0000"]W[/color][color="#006400"]i[/color][color="#0000FF"]k[/color][color="#FFA500"]i[/color] contains full upload file to DB and open/retrieve it from DB tutorial.

Tom