Yii Framework Forum: Action view issue when displaying images - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Action view issue when displaying images Rate Topic: -----

#1 User is offline   Vaibhav 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 55
  • Joined: 23-August 12
  • Location:Berlin

Posted 24 August 2012 - 07:13 AM

I am creating a simple registration form with 3 text boxes and 1 drop down box. However the form has an image upload option. After following the instruction given in the YII wiki http://www.yiiframew...-stored-images/ . I am able to successfully upload the image and my database is also making the entries. Secondly, localhost/(my app)/(controller)/loadimage&id=2 displays the image As given in the wiki page.

However, I get an "htmlspecialchars() [<a href='function.htmlspecialchars'>function.htmlspecialchars</a>]: Invalid multibyte sequence in argument" error when after CRUD create option the view action is invoked. For ex. localhost/(my app)/(controller)/view&id=2 gives me the error. Looking at the stack information

-------------------------------------------------------------------------------------------------------------------------
#9
– /Applications/MAMP/htdocs/iow/protected/controllers/ReportController.php(55): CController->renderPartial("view", array("model" => Report))
50 */
51 public function actionView($id)
52 {
53 $this->renderPartial('view',array(
54 'model'=>$this->loadModel($id),
55 ));
56 }
57
58 /**
59 * Creates a new model.
60 * If creation is successful, the browser will be redirected to the 'view' page.
----------------------------------------------------------------------------------------------------------------------------
#10
unknown(0): ReportController->actionView("11")
#11


Attached File  Screen Shot 2012-08-24 at 2.04.47 PM.png (67.35K)
Number of downloads: 9

My view.php file looks like this

<?php
$this->breadcrumbs=array(
'Reports'=>array('index'),
$model->id,
);

$this->menu=array(
array('label'=>'List Report', 'url'=>array('index')),
array('label'=>'Create Report', 'url'=>array('create')),
array('label'=>'Update Report', 'url'=>array('update', 'id'=>$model->id)),
array('label'=>'Delete Report', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),
array('label'=>'Manage Report', 'url'=>array('admin')),
);
?>

<h1>View Report #<?php echo $model->id; ?></h1>

<?php

$this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'registration',
'vehicle_type',
'violation_type',
'city',
'state',
'description',
'binaryfile',
'filename',
'filetype',
'user',
'create_time',
),
)); ?>



My _view.php file looks like this

<div class="view">

<b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>
<?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ?>
<br />

<b><?php echo CHtml::encode($data->getAttributeLabel('registration')); ?>:</b>
<?php echo CHtml::encode($data->registration); ?>
<br />

<b><?php echo CHtml::encode($data->getAttributeLabel('violation_type')); ?>:</b>
<?php echo CHtml::encode($data->violation_type); ?>
<br />

<b><?php echo CHtml::encode($data->getAttributeLabel('city')); ?>:</b>
<?php echo CHtml::encode($data->city); ?>
<br />

<b><?php echo CHtml::encode($data->getAttributeLabel('state')); ?>:</b>
<?php echo CHtml::encode($data->state); ?>
<br />

<b><?php echo CHtml::encode($data->getAttributeLabel('description')); ?>:</b>
<?php echo CHtml::encode($data->description); ?>
<br />

<?php echo CHtml::image(Yii::app()->controller->createUrl('report/loadImage', array('id'=>$this->model->id)));?>



I would be grateful, if some of you can advice how I should go about rendering the image along with form data as we see in the default view action case.
0

#2 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 752
  • Joined: 02-February 09

Posted 24 August 2012 - 07:33 AM

start commenting section by section from your view file and keep reproducing the error until you find the portion of code that is causing problems
0

#3 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 04-March 10
  • Location:UK

Posted 24 August 2012 - 07:50 AM

Change
    'binaryfile',


to
    array(
        'name'=>'binaryfile',
        'type'=>'raw',
    ),


That should prevent Yii from attempting to encode the binary data.
0

#4 User is offline   Vaibhav 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 55
  • Joined: 23-August 12
  • Location:Berlin

Posted 24 August 2012 - 08:23 AM

Hey keith,

Thanks. The view is rendering now. Howver, All I see is garbled data in the coloum that should show the image.

Attached File  Screen Shot 2012-08-24 at 3.19.37 PM.png (561.4K)
Number of downloads: 5

Whereas controler/loadimage&11 shows the image.

Attached File  Screen Shot 2012-08-24 at 3.21.42 PM.png (384.1K)
Number of downloads: 5


View PostVaibhav, on 24 August 2012 - 08:12 AM, said:

Hi Keith,

Where should i make this change could you specify?

Vaibhav

0

#5 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 04-March 10
  • Location:UK

Posted 24 August 2012 - 08:30 AM

Yep, that makes sense because the browser doesn't know what to do with the binary data. If you don't need to show it in that view, your best bet is probably to remove that column.

It's easier to store images on the file system and store just the file path in the database. That way you can render an img tag to show the image anywhere you like.

If you're intent on keeping the binary data in the database, you'll probably need to make a special controller and action to output the image data and point your img tag's url to that. In that case, you'll need to take charge of ensuring the correct headers are sent for the file type to ensure that the browser displays the image correctly and doesn't attempt any sort of file download. I take it this is the purpose of your loadimage action.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users