Skip to content

Commit

Permalink
piter
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrnov committed Aug 9, 2016
1 parent 157774d commit 9410839
Show file tree
Hide file tree
Showing 7 changed files with 593 additions and 3 deletions.
70 changes: 70 additions & 0 deletions DownloadAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* User: Scott_Huang
* Date: 6/16/2015
* Time: 5:48 PM
*/

namespace ptrnov\postman4excel;

use Yii;
use yii\base\Action;
class DownloadAction extends Action
{
public function run($file_name, $file_type = 'excel', $deleteAfterDownload = true) {
if (empty($file_name)) {
// return $this->goBack();
return 0;
}
$baseRoot=Yii::getAlias('@vendor').'/ptrnov/yii2-postman4excel/tmp';
//$baseRoot=dirname(dirname(__DIR__)).'/cronjob/';
//$baseRoot = Yii::getAlias('@webroot') . "/uploads/";
$file_name = $baseRoot . $file_name;
//echo $file_name,"<BR/>";
if (!file_exists($file_name)) {
//HzlUtil::setMsg("Error", "File not exist");
return 0;
}
$fp = fopen($file_name, "r");
$file_size = filesize($file_name);
//下载文件需要用到的头
if ($file_type == 'excel') {
header('Pragma: public');
header('Expires: 0');
header('Content-Encoding: none');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: public');
header('Content-Type: application/vnd.ms-excel');
header('Content-Description: File Transfer');
Header("Content-Disposition: attachment; filename=" . basename($file_name));
header('Content-Transfer-Encoding: binary');
Header("Content-Length:" . $file_size);
} else if ($file_type == 'picture') { //pictures
Header("Content-Type:image/jpeg");
Header("Accept-Ranges: bytes");
Header("Content-Disposition: attachment; filename=" . basename($file_name));
Header("Content-Length:" . $file_size);
} else { //other files
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Content-Disposition: attachment; filename=" . basename($file_name));
Header("Content-Length:" . $file_size);
}

$buffer = 1024;
$file_count = 0;
//向浏览器返回数据
while (!feof($fp) && $file_count < $file_size) {
$file_con = fread($fp, $buffer);
$file_count+=$buffer;
echo $file_con;
}
//echo fread($fp, $file_size);
fclose($fp);
if ($deleteAfterDownload) {
// unlink($file_name);
}
return 1;
}

}
Loading

0 comments on commit 9410839

Please sign in to comment.