Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I trigger a download with typo3/extbase?

I'm using Typo3 with extbase and fluid. I have a Controller with an action called downloadAction(). After calling the action the system tries to render the download-template (but I just want to start a download).

public function downloadAction($id) {
  // create file
  // send header
  // dump file
  // exit
}

How can I dump the created download-File and send a download header instead of the normal render process? What is the best way?

Thanks

like image 910
koalabruder Avatar asked Dec 02 '22 02:12

koalabruder


2 Answers

I did this in a project some months ago, it's pretty straight forward.

    /**
     * @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;   
    }
like image 105
Alex Avatar answered Dec 18 '22 21:12

Alex


You can define a special PageType for download requests:

download = PAGE
download  {
  typeNum = 1249058993
  10 < tt_content.list.20.efempty_pi1

  config {
   disableAllHeaderCode = 1
   xhtml_cleaning = 0
   admPanel = 0
   additionalHeaders = Content-type:application/octet-stream
    }
  }
}

"efempty" must be replaced by your extension's name.

You are now able to trigger the download like this:

<f:link.action pageType="1249058993" action="download" controller="ControllerName">Download</f:link.action>
like image 24
xandi Avatar answered Dec 18 '22 22:12

xandi