【问题标题】:TYPO3 extbase: send correct response headers for downloadsTYPO3 extbase:为下载发送正确的响应标头
【发布时间】:2017-11-10 23:29:30
【问题描述】:

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

打字稿:

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 控制器操作

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 在某处被覆盖。

【问题讨论】:

    标签: typo3


    【解决方案1】:

    通过 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;
        }
    

    【讨论】:

    • TypoScript config.additionalHeaders = Content-Type:text/html;charset=utf-8 否决了 setContentType()
    【解决方案2】:

    您是否尝试使用“additionalHeaders”在 TypoScript 中设置标题?

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

    【讨论】:

    • 如果内容类型是固定的,那将起作用。但它可能会有所不同(doc、pdf、...) - 好吧,这在我硬编码的测试代码中并不明显。
    【解决方案3】:

    我是这样做的:在 Controller 的 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;
        }
    

    【讨论】:

    • 抱歉,这不适用于 extbase 框架。退出一个动作可能不是最好的主意。而且由于有设置 HTTP 标头的方法,我倾向于使用它们。但它是一个有效的 PHP 解决方案(当您添加输出文件内容的行时)。
    • 好吧,也许你是对的。但是,它对我来说效果很好。 (添加了输出文件的行)。
    猜你喜欢
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-20
    • 1970-01-01
    • 2019-04-29
    相关资源
    最近更新 更多