2017-11-10 15 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に:

/** @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; 
    } 
0

'additionalHeaders'を使ってTypoScriptでヘッダーを設定しようとしましたか?

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

コンテンツタイプが修正されました。しかし、それは変わる可能性があります(doc、pdf、...) - OK、それはハードコードされたテストコードからは分かりません。 –

0

私はこのようにそれを行う:コントローラTypoScriptFrontendController(TSFE)を経由して右のContent-typeを設定し

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

関連する問題