php - 如何使用 typo3/extbase 触发下载?

标签 php typo3 extbase fluid

我将 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/

相关文章:

php - 如何实现工厂设计模式

php - Heroku 上的 Symfony : 403 Forbidden You don't have permission to access/on this server

php - 如何在TYPO3后端写入日志?

typo3 - 如何在typo3版本10中执行纯SQL($GLOBALS ['TYPO3_DB']->sql_query())?

php - 哪个对数组处理得更好?

php - 在 PHP 中解析 HTTP_RANGE header

html - TYPO3 6.2 RTE 无法添加按键

typo3 - 从 BE 注销后,我注册的命名空间停止工作

php - 自 TYPO3 6.2 起,添加静态 SQL 表及其数据不起作用

TYPO3 TCA 选择,项目数组中的 NULL 值