Download file using Yii::app()->getRequest()->sendFile

Hi there.

I am complete noob, so please forgive my ignorance.

I have searched in the yii documentation, the yii cookbook, and online for a tutorial that will show me how to do this, but I have yet to find a solution. I’m sure it’s pretty simple, I would appreciate any help.

I have a facility whereby the admin user can upload files to the server. I now need to make a link where that file is available for download by any user using CHttpRequest’s sendFile() function. The link will be located in the side menu (using ‘views/layouts/column2.php’.)

The code on my view:




$link = '<div id="schedule_download"></div>';

echo CHtml::link($link ,array('/admin/download'));



The code in my controller:




public function actionDownload()

{

  //find the last uploaded file  

  $criteria=new CDbCriteria

  $criteria->order = 'admin_id DESC';

  $criteria->limit = '1';

  $downloads = Admin::model()->findAll($criteria);

  foreach ($downloads as $download):

    $path = Yii::app()->request->hostInfo . Yii::app()->request->baseURL . '/uploads/schedule/' . $download->schedule;

  endforeach;




  if(file_exists($path))

  {

    return Yii::app()->getRequest()->sendFile('myfile.pdf', @file_get_contents($path));

  }

}



When I click on the link it takes me to a blank page.

I would merely like the file download dialogue to appear and no other page re-directs.

Please can somebody show me where I am going wrong, or provide a link to a tutorial that will explain this process better. Thanks :)

don’t know how others do :lol:

here will be my may:

a link for download




<a  href="<?php echo $this->createUrl('someDownloadAction','fileId'=>'someXXX') ;?>"  target="helperFrame" > downloadIt </a>

<iframe src="" style="display:none" name="helperFrame"/>



above using hidden iframe as the link 's target . so the page will be not jumped .

then in you someAction of your controller just according to the "fileId" to look up the filePath then send it (the file path normally stored in some tables ).

Thank you, it worked!

I could not use


$this->createUrl()

in the sideMenu, you have to use:


Yii::app()->createUrl...

I did not end up using the target iframe. Just calling the actionDownload function which returns the file using:


Yii::app()->getRequest()->sendFile

worked just fine.

I really appreciate the help. :)

Hi yiqing95,

Where should i place the code? and what is that someDownloadAction and field id?





                      Thank you,


                      Ananth.G

Here is a code for Download controller.

My url is like:

index.php/download/download?name=AILET+Sample+Form1&id=53&file=1381053929_pita.gif

public function actionDownload(){

			 &#036;model = new Download; 


			 &#036;name	= &#036;_GET['file'];				


			 &#036;upload_path = Yii::app()-&gt;params['uploadPath'];		


				


				if( file_exists( &#036;upload_path.&#036;name ) ){


					    Yii::app()-&gt;getRequest()-&gt;sendFile( &#036;name , file_get_contents( &#036;upload_path.&#036;name ) );


				}


				else{


					    &#036;this-&gt;render('download404');


				}	


			


		}

Thanks Mayank! I found this solution helpful to me.

Perfect saved me a lot Thanks

Very helpful post - thanks. Note the syntax for sending the file is a bit different for Yii 2.0…

    if(file_exists(&#036;path)) {


        [b]Yii::&#036;app-&gt;response-&gt;sendFile(&#036;path);[/b]

Hi,

I did in the following manner


$dir_path = Yii::getPathOfAlias('webroot') . '/uploads/resums/';


$fileName = $dir_path . "/$name";




if (file_exists($fileName))

			return Yii::app()->getRequest()->sendFile($name, @file_get_contents($fileName));




else

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

How to creat a files repository

Upload file its easy!

This video teach how to do this.

www youtube com/watch?v=N6xhrsNpT5U&index=3&list=PLyHUg4UaA4fG96XpW7F2YQiTfr_NSMdhI

www youtube com/watch?v=NSbE3rZKaIQ&index=4&list=PLyHUg4UaA4fG96XpW7F2YQiTfr_NSMdhI

And if do you know to do Download Files

www youtube.com/watch?v=37g7i48-8fE&index=5&list=PLyHUg4UaA4fG96XpW7F2YQiTfr_NSMdhI

hwww youtube com/watch?v=Djz90F2_oAk&list=PLyHUg4UaA4fG96XpW7F2YQiTfr_NSMdhI&index=6

I also faced the same issue. And is resolved, if the pjax is turned off in the grid view page. If the pjax is enabled, the download file renders in the same page, considering the request is of ajax