So basically I have a controller that handles files (images & videos) and on the "view" view I have a button that's called "view content". That button is using the controller actionContent($id)
and I want it when pressed to run the AJAX call to the action & render in-page in a div
the image that's located in the model with id == $id
. How can I handle the AJAX request and return the image displayed in the div
?
public function actionContent($id)
{
$model = $this->findModel($id);
switch (substr($model->mime, 0, strpos($model->mime, '/'))) {
case 'image' :
return $this->renderAjax('_image', [
'img' => $model->getFullPath(),
]);
break;
default:
break;
}
}
And the view:
<span class="pull-right">
<?= Html::a('View Content', ['content', 'id' => $model->id], [
'class' => 'btn btn-default',
'id' => 'content'
]); ?>
</span>
With the script I already tried:
$(document).ready(function () {
$('#content').click(function () {
$(this).preventDefault();
$.ajax({
type: "GET",
url: $(this).href,
contentType: "<?= $model->mime?>",
success: function (response) {
('.show-content').html('<img src="data:image/png;base64,' + response + '" />');
}
})
})
})