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

Jump to content

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

Download file using Yii::app()->getRequest()->sendFile File Downloads using sendFile Rate Topic: -----

#1 User is offline   ilovebutternut 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 19-June 12
  • Location:South Africa

  Posted 19 June 2012 - 08:42 AM

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 :)
1

#2 User is offline   yiqing95 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 608
  • Joined: 27-December 10
  • Location:china

Posted 19 June 2012 - 11:12 AM

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 ).
0

#3 User is offline   ilovebutternut 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 19-June 12
  • Location:South Africa

Posted 20 June 2012 - 07:49 AM

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. :)
0

#4 User is offline   ananthguru007 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 08-August 13

Posted 27 August 2013 - 04:18 AM

View Postyiqing95, on 19 June 2012 - 11:12 AM, said:

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 ).



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

Thank you,
Ananth.G
0

#5 User is offline   Mayank Singh Parmar 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 28-February 13

Posted 07 October 2013 - 12:04 PM

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(){

$model = new Download;
$name = $_GET['file'];
$upload_path = Yii::app()->params['uploadPath'];

if( file_exists( $upload_path.$name ) ){
Yii::app()->getRequest()->sendFile( $name , file_get_contents( $upload_path.$name ) );
}
else{
$this->render('download404');
}

}
2

#6 User is offline   Vicer 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 06-November 12

Posted 21 November 2013 - 12:19 AM

View PostMayank Singh Parmar, on 07 October 2013 - 12:04 PM, said:

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(){

$model = new Download;
$name = $_GET['file'];
$upload_path = Yii::app()->params['uploadPath'];

if( file_exists( $upload_path.$name ) ){
Yii::app()->getRequest()->sendFile( $name , file_get_contents( $upload_path.$name ) );
}
else{
$this->render('download404');
}

}



Thanks Mayank! I found this solution helpful to me.
0

#7 User is offline   Jawwad Ahmed 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 44
  • Joined: 18-November 13

Posted 09 March 2014 - 03:32 AM

View PostMayank Singh Parmar, on 07 October 2013 - 12:04 PM, said:

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(){

$model = new Download;
$name = $_GET['file'];
$upload_path = Yii::app()->params['uploadPath'];

if( file_exists( $upload_path.$name ) ){
Yii::app()->getRequest()->sendFile( $name , file_get_contents( $upload_path.$name ) );
}
else{
$this->render('download404');
}

}



Perfect saved me a lot Thanks
0

#8 User is offline   Paul Scivetti 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 09-March 14

Posted 10 March 2014 - 12:54 AM

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


if(file_exists($path)) {
Yii::$app->response->sendFile($path);
0

#9 User is offline   Nisanth thulasi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 74
  • Joined: 10-July 12
  • Location:Kochi

Posted 22 March 2014 - 05:42 AM

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.');

Thanks
Nisanth Thulasi
1

#10 User is offline   Aluno Computacao on youtub 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 23-September 17

Posted 23 September 2017 - 10:53 PM

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
0

Share this topic:


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

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