mirror of https://gitee.com/karson/fastadmin.git
Pre Merge pull request !424 from 码龍/AddDownloadResponse
commit
c983c89b1f
|
|
@ -507,3 +507,19 @@ EOT;
|
|||
return $icon;
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('download')) {
|
||||
/**
|
||||
* 获取\think\response\Download对象实例
|
||||
* @param string $filename 要下载的文件
|
||||
* @param string $name 显示文件名
|
||||
* @param bool $content 是否为内容
|
||||
* @param integer $expire 有效期(秒)
|
||||
* @param bool $openinBrowser 设置是否在浏览器中显示文件
|
||||
* @return \think\response\Download
|
||||
*/
|
||||
function download($filename, $name = '', $content = false, $expire = 360, $openinBrowser = false)
|
||||
{
|
||||
return (new \app\common\library\response\Download($filename))->name($name)->isContent($content)->expire($expire)->openinBrowser($openinBrowser);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,148 @@
|
|||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\common\library\response;
|
||||
|
||||
use think\Response;
|
||||
use think\Exception;
|
||||
|
||||
class Download extends Response
|
||||
{
|
||||
protected $expire = 360;
|
||||
protected $name;
|
||||
protected $mimeType;
|
||||
protected $isContent = false;
|
||||
protected $openinBrowser = false;
|
||||
/**
|
||||
* 处理数据
|
||||
* @access protected
|
||||
* @param mixed $data 要处理的数据
|
||||
* @return mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function output($data)
|
||||
{
|
||||
if (!$this->isContent && !is_file($data)) {
|
||||
throw new Exception('file not exists:' . $data);
|
||||
}
|
||||
|
||||
ob_end_clean();
|
||||
|
||||
if (!empty($this->name)) {
|
||||
$name = $this->name;
|
||||
} else {
|
||||
$name = !$this->isContent ? pathinfo($data, PATHINFO_BASENAME) : '';
|
||||
}
|
||||
|
||||
if ($this->isContent) {
|
||||
$mimeType = $this->mimeType;
|
||||
$size = strlen($data);
|
||||
} else {
|
||||
$mimeType = $this->getMimeType($data);
|
||||
$size = filesize($data);
|
||||
}
|
||||
|
||||
$this->header['Pragma'] = 'public';
|
||||
$this->header['Content-Type'] = $mimeType ?: 'application/octet-stream';
|
||||
$this->header['Cache-control'] = 'max-age=' . $this->expire;
|
||||
$this->header['Content-Disposition'] = $this->openinBrowser ? 'inline' : 'attachment; filename="' . $name . '"';
|
||||
$this->header['Content-Length'] = $size;
|
||||
$this->header['Content-Transfer-Encoding'] = 'binary';
|
||||
$this->header['Expires'] = gmdate("D, d M Y H:i:s", time() + $this->expire) . ' GMT';
|
||||
|
||||
$this->lastModified(gmdate('D, d M Y H:i:s', time()) . ' GMT');
|
||||
|
||||
$data = $this->isContent ? $data : file_get_contents($data);
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置是否为内容 必须配合mimeType方法使用
|
||||
* @access public
|
||||
* @param bool $content
|
||||
* @return $this
|
||||
*/
|
||||
public function isContent($content = true)
|
||||
{
|
||||
$this->isContent = $content;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置有效期
|
||||
* @access public
|
||||
* @param integer $expire 有效期
|
||||
* @return $this
|
||||
*/
|
||||
public function expire($expire)
|
||||
{
|
||||
$this->expire = $expire;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置文件类型
|
||||
* @access public
|
||||
* @param string $filename 文件名
|
||||
* @return $this
|
||||
*/
|
||||
public function mimeType($mimeType)
|
||||
{
|
||||
$this->mimeType = $mimeType;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件类型信息
|
||||
* @access public
|
||||
* @param string $filename 文件名
|
||||
* @return string
|
||||
*/
|
||||
protected function getMimeType($filename)
|
||||
{
|
||||
if (!empty($this->mimeType)) {
|
||||
return $this->mimeType;
|
||||
}
|
||||
|
||||
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
||||
|
||||
return finfo_file($finfo, $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置下载文件的显示名称
|
||||
* @access public
|
||||
* @param string $filename 文件名
|
||||
* @param bool $extension 后缀自动识别
|
||||
* @return $this
|
||||
*/
|
||||
public function name($filename, $extension = true)
|
||||
{
|
||||
$this->name = $filename;
|
||||
|
||||
if ($extension && false === strpos($filename, '.')) {
|
||||
$this->name .= '.' . pathinfo($this->data, PATHINFO_EXTENSION);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置是否在浏览器中显示文件
|
||||
* @access public
|
||||
* @param bool $openinBrowser 是否在浏览器中显示文件
|
||||
* @return $this
|
||||
*/
|
||||
public function openinBrowser($openinBrowser) {
|
||||
$this->openinBrowser = $openinBrowser;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue