I have placed a PDF file in folder /protected/uploads. I want to view this file on click of hyperlink. But i am facing the error and the PDF is not being displayed.
Error
Error 404
Unable to resolve the request "uploads/viewPdf".
Here is what i have done to view the file.
main.php
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'uploads/<filename:[a-zA-Z]+\.pdf>' => 'Upload/viewPdf',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
UploadController
class UploadController extends Controller
{
public function actionIndex()
{
$this->render('index');
}
public function actionViewPdf()
{
$filename = $_GET['filename'] . '.pdf';
$filepath = '/uploads/Tutorial.pdf' . $filename;
if(file_exists($filepath))
{
// Set up PDF headers
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($filepath));
header('Accept-Ranges: bytes');
// Render the file
readfile($filepath);
}
else
{
// PDF doesn't exist so throw an error or something
}
}
}
and the link in the form. I would like to mention here is that the form belong to other controller not the upload controller
_form
echo CHtml::link(
'pdf',
Yii::app()->createUrl('/uploads/viewPdf', array('filename' => 'Tutorial')) ,
array('class'=>'btnPrint btn btn-danger','target'=>'_blank'));