TYPO3 extbase:为下载发送正确的响应标头

时间:2017-11-10 09:16:08

标签: typo3

我想向用户发送下载文件,但HTTP标头发送错误。我希望发送Content-type: application/octet-stream,但我仍然会Content-Type: text/html; charset=utf-8。有人可以指出我的错误吗? TYPO3 7.6.22

的TypoScript:

page_1505214798 = PAGE
page_1505214798 {
    typeNum = 1505214798
    config {
        contentObjectExceptionHandler = 0
        xhtml_cleaning = 0
        admPanel = 0
        disableAllHeaderCode = 1
        additionalHeaders {
        }
        debug = 0
    }
    10 = USER_INT
    10 {
        userFunc = TYPO3\CMS\Extbase\Core\Bootstrap->run
        vendorName = DIX
        extensionName = Dixeventmanager
        pluginName = Meeting
        controller = Event
        action = download
        switchableControllerActions {
            Event {
                1 = download
            }
        }
    }
}

Extbase Controller Action

public function downloadAction() {
    // $fn = ...
    $result = file_get_contents(PATH_site . $fn);
    $this->response->setHeader('Content-type', 'application/octet-stream'); 
    $this->response->setHeader('Content-Disposition', 'attachment; filename="'. basename($fn) .'"'); 
    return $result;
}

正确发送带有文件名的Content-disposition标头,只是在某处覆盖了Content-type。

3 个答案:

答案 0 :(得分:2)

您是否曾尝试使用' additionalHeaders'设置TypoScript中的标题?

additionalHeaders {
   10 {
      header = Content-Type: application/octet-stream
      replace = 1
   }
}

答案 1 :(得分:2)

通过TypoScriptFrontendController(TSFE)设置正确的Content-type:

/** @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController $typoScriptFrontendController */
$typoScriptFrontendController = $GLOBALS['TSFE'];
$typoScriptFrontendController->setContentType('image/jpeg');

以下是一个示例动作:

    public function downloadAction() {
        $filename = '/path/to/my/file.jpg';
        $file = file_get_contents($filename);

        /** @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController $typoScriptFrontendController */
        $typoScriptFrontendController = $GLOBALS['TSFE'];
        $typoScriptFrontendController->setContentType('image/jpeg');

        $this->response->setHeader('Content-Transfer-Encoding: binary');
        $this->response->setHeader('Content-Disposition', 'attachment; filename="' . basename($filename) . '"');

        return $file;
    }

答案 2 :(得分:0)

我是这样做的:在一个控制器的downloadAction中

    if (file_exists($dlPath)) {
        $filesize = filesize($dlPath);

        $mimetype = $fileReference->getMimeType();

        switch ($this->settings['downloadMode']) {
            case 'inline':
                $contentDispostion = "inline";
                break;
            default:
                $contentDispostion = "attachment";
        }

        //  stream file
        header('Content-Description: File Transfer');
        header('Content-Type: ' . $mimetype);
        header('Content-Disposition: ' . $contentDispostion . '; filename="' . ($showname) . '"');
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . $filesize);
        if (ob_get_level()) {
            ob_end_clean();
        }
        readfile($dlPath);
        exit;
    }