我将 Typo3 与 extbase 和 fluid 一起使用。我有一个 Controller ,其操作名为 downloadAction()
。调用操作后,系统会尝试呈现下载模板(但我只想开始下载)。
public function downloadAction($id) {
// create file
// send header
// dump file
// exit
}
如何转储创建的 download-File
并发送下载 header 而不是正常的渲染过程?什么是最好的方法?
谢谢
最佳答案
我几个月前在一个项目中做过这个,它非常简单。
/**
* @param string $fileName
* @return void
*/
public function downloadAction($fileName) {
$file = $this->settings['uploadFolder'] . 'uploadedPhotos/' . $fileName;
if(is_file($file)) {
$fileLen = filesize($file);
$ext = strtolower(substr(strrchr($fileName, '.'), 1));
switch($ext) {
case 'txt':
$cType = 'text/plain';
break;
case 'pdf':
$cType = 'application/pdf';
break;
case 'zip':
$cType = 'application/zip';
break;
case 'doc':
$cType = 'application/msword';
break;
case 'xls':
$cType = 'application/vnd.ms-excel';
break;
case 'ppt':
$cType = 'application/vnd.ms-powerpoint';
break;
case 'gif':
$cType = 'image/gif';
break;
case 'png':
$cType = 'image/png';
break;
case 'jpeg':
case 'jpg':
$cType = 'image/jpg';
break;
case 'mp3':
$cType = 'audio/mpeg';
break;
case 'wav':
$cType = 'audio/x-wav';
break;
case 'mpeg':
case 'mpg':
case 'mpe':
$cType = 'video/mpeg';
break;
case 'mov':
$cType = 'video/quicktime';
break;
case 'avi':
$cType = 'video/x-msvideo';
break;
//forbidden filetypes
case 'inc':
case 'conf':
case 'sql':
case 'cgi':
case 'htaccess':
case 'php':
case 'php3':
case 'php4':
case 'php5':
exit;
case 'exe':
default:
$cType = 'application/octet-stream';
break;
}
$headers = array(
'Pragma' => 'public',
'Expires' => 0,
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'Content-Description' => 'File Transfer',
'Content-Type' => $cType,
'Content-Disposition' => 'attachment; filename="'. $fileName .'"',
'Content-Transfer-Encoding' => 'binary',
'Content-Length' => $fileLen
);
foreach($headers as $header => $data)
$this->response->setHeader($header, $data);
$this->response->sendHeaders();
@readfile($file);
}
exit;
}
关于php - 如何使用 typo3/extbase 触发下载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5554211/