2017-11-10 1 views
0

사용자에게 다운로드 파일을 보내고 싶지만 HTTP 헤더가 잘못 전송됩니다. Content-type: application/octet-stream이 전송 될 것으로 예상되지만 여전히 Content-Type: text/html; charset=utf-8이 표시됩니다. 누군가 내 실수를 저에게 지적 할 수 있습니까? TYPO3 7.6.22TYPO3 extbase : 다운로드에 대한 올바른 응답 헤더를 보내십시오.

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 컨트롤러 액션

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

파일 이름과 내용 - 처리 헤더가 바로 컨텐츠 형은 어딘가 덮어 제대로 전송됩니다.

답변

2

의 downloadAction에서 :

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

'additionalHeaders'를 사용하여 TypoScript에서 헤더를 설정하려고 했습니까?

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

콘텐츠 유형이 수정되었습니다. 그러나 그것은 다양 할 수 있습니다. (doc, pdf, ...) - OK, 테스트 코드에서 하드 코딩 된 것이 명확하지 않습니다. –

0

나는 이런 식으로 그것을 할 : 여기

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

는 예를 들어 액션의 : TypoScriptFrontendController (TSFE)을 통해 올바른 콘텐츠 유형을 설정합니다 컨트롤러

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

죄송합니다. 이것은 extbase 프레임 워크와 잘 작동하지 않습니다. 액션에서 나가는 것이 가장 좋은 방법은 아닙니다. 그리고 HTTP 헤더를 설정하는 방법이 있기 때문에 나는 그것을 사용하는 경향이있다. 하지만 유효한 PHP 솔루션입니다 (파일 내용을 출력하는 라인을 추가 할 때). –

+0

좋습니다. 아마 맞을 수도 있습니다. 그러나, 그것은 나를 위해 잘 작동합니다. (파일을 출력하는 줄을 추가했습니다). – Martin

관련 문제