Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
TYPO3 extbase:为下载发送正确的响应标题_Typo3 - Fatal编程技术网

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

TYPO3 extbase:为下载发送正确的响应标题,typo3,Typo3,我想向用户发送下载文件,但HTTP头发送错误。我希望内容类型:application/octet stream被发送,但我仍然得到内容类型:text/html;字符集=utf-8。有人能指出我的错误吗? 类型3 7.6.22 打字稿: page_1505214798 = PAGE page_1505214798 { typeNum = 1505214798 config { contentObjectExceptionHandler = 0 xht

我想向用户发送下载文件,但HTTP头发送错误。我希望
内容类型:application/octet stream
被发送,但我仍然得到
内容类型:text/html;字符集=utf-8
。有人能指出我的错误吗? 类型3 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;
}

带有文件名的内容处置头被正确发送,只是内容类型在某个地方被覆盖

您是否尝试使用“additionalHeaders”设置打字脚本中的标题

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

您是否尝试使用“additionalHeaders”设置打字稿中的标题

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

我是这样做的:在一个控制器的下载动作中

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

我是这样做的:在一个控制器的下载动作中

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

通过TypoScriptFrontendController(TSFE)设置正确的内容类型:

下面是一个示例操作:

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

通过TypoScriptFrontendController(TSFE)设置正确的内容类型:

下面是一个示例操作:

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

如果内容类型是固定的,那就行了。但是它可能会有所不同(doc,pdf,…)-好的,从我硬编码的测试代码来看,这并不明显。如果内容类型是固定的,那就行了。但是它可能会有所不同(doc,pdf,…)-好的,从我硬编码的测试代码来看,这并不明显。在行动中退出可能不是最好的主意。由于有设置HTTP头的方法,我倾向于使用它们。但是它是一个有效的PHP解决方案(当您添加输出文件内容的行时)。然而,它对我来说很有效。(添加了输出文件的行)。抱歉,这在extbase框架中不起作用。在行动中退出可能不是最好的主意。由于有设置HTTP头的方法,我倾向于使用它们。但是它是一个有效的PHP解决方案(当您添加输出文件内容的行时)。然而,它对我来说很有效。(添加了输出文件的行);charset=utf-8否决了setContentType()打字脚本配置。additionalHeaders=内容类型:text/html;charset=utf-8否决了setContentType()