contao/core-bundle

Bug : Download in news reader

Closed this issue · 8 comments

Hi,

We can't use content download or content downloads in a news because the file are corrupt when downloading.

A part of the template are injected in the header and we don't have the end part of the file.

I found this issue on the demo site too. I've not test on Contao 4.0 but i've the same issue since Contao 4.1

Header of a PDF file for exemple:


<div class="layout_full block" itemscope itemtype="http://schema.org/Article">

  <h1 itemprop="name">Hello world</h1>

      <p class="info"><time datetime="2017-01-30T17:27:00+01:00" itemprop="datePublished">2017-01-30 17:27</time> par Admin  </p>
  
  
      %PDF-1.5
%âãÏÓ
10 0 obj
<</Linearized 1/L 134289/O 12/E 129957/N 1/T 133984/H [ 777 289]>>
endobj
               
90 0 obj
<</DecodeParms<</Columns 5/Predictor 12>>/Filter/FlateDecode/ID[<E182E5868FCF0E01A64FE1F579ECD28B><BC0DE23470124040AC4DE59087625AF3>]/Index[10 135]/Info 9 0 R/Length 209/Prev 133985/Root 11 0 R/Size 145/Type/XRef/W[1 3 1]>>stream
hÞbbd`�``b``‘�‘~ ’q�ˆä¬�‘<�A$×S°H�˜¼	"ÙÝ@$·�ˆäð�³Áê¹[@¤À�°9µ`2�D��ƒH¦n°-ý@’1ì'X�l;ã��™���‘ ‹$�H�o0»�Lšƒõž�³Áz�ç�Éû`R�L
!±1e�ÑU2ÌER#„ ™„ÐE�zÀì{`R˜��!â�ßÍ�›€ä�&°	��í@ø�Ó^dó���ÙH$�"ÿY��˜€±q��ã(I2ùŸ�ùÏ�€ � Øã)�
endstream
endobj
startxref
0
%%EOF...............

Best

I can confirm.
Fortunately news-attachment is not affected.

Which Contao version does this issue occur in?

Whatever version the official-demo is running. i did't test 3.5.

@leofeyer C 4.1.x 4.2.x 4.3.x
I don't know for C 4.0.x,
I don't have the bug on C 3.5.x

i've the solution

We need too check if no output buffer is active:
In library/Contao/File.php

/**
	 * Send the file to the browser
	 *
	 * @param string $filename An optional filename
	 *
	 * @throws ResponseException
	 */
	public function sendToBrowser($filename='')
	{
		// Make sure no output buffer is active
		// @see http://ch2.php.net/manual/en/function.fpassthru.php#74080
		while (@ob_end_clean());

		$response = new BinaryFileResponse(TL_ROOT . '/' . $this->strFile);

		$response->setContentDisposition
		(
			ResponseHeaderBag::DISPOSITION_ATTACHMENT,
			$filename,
			$this->basename
		);

		$response->headers->addCacheControlDirective('must-revalidate');
		$response->headers->addCacheControlDirective('post-check', 0);
		$response->headers->addCacheControlDirective('pre-check', 0);

		$response->headers->set('Connection', 'close');

		throw new ResponseException($response);
	}

How do I reproduce this in the online demo?

Adds a download content to the detail of a news item. And try to download the file

Closed in favor of #698.