Download excel files stored in the server

hi all,

i am trying to download excel files that i have uploaded to the server using web form
i am using ubuntu 10.04, symfony 1.4 and doctrine 1.2

when i click the link, it downloads, but a corrupted file. the code is as follows


public function executeDownload(sfWebRequest $request)
	{
		$excel_file = Doctrine::getTable('Project')->find($request->getParameter('id'));

		$this->setLayout(false);
		sfConfig::set('sf_web_debug', false);

		$excelpath = sfConfig::get('sf_upload_dir').DIRECTORY_SEPARATOR.'project'.DIRECTORY_SEPARATOR.$excel_file['file'];

		// check if the file exists
		$this->forward404Unless(file_exists($excelpath));
		
		$response = $this->getContext()->getResponse();
		$response->setHttpHeader('Pragma', '');
		$response->setHttpHeader('Cache-Control', '');
		$response->setHttpHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
		$response->setHttpHeader('Content-Disposition', 'attachment; filename="invoice.xlsx"');
		$response->setContent($excel_file);
		
		
		return sfView::NONE;
		
	}

i have checked the original uploaded file and it seems to be ok. I have also checked
for available mimetypes at /etc/mime.types and support for 2007 excel format is there

But i can’t figure out why the download has a problem.

Any help please

Have you compared the content of the file and response ? Maybe your application is adding whitespace or some other content, try “diff’ing” the two.

by response, do you mean the resulting downloaded file?
what is downloaded is corrupt, so its not the same file stored in the server

Yes.

I understand that is is corrupt. By looking at a diff of the two, you should hopefully see why it is corrupt. :slight_smile:

Wait a moment, shouldn’t that method read something like:-


<?php
public function executeDownload(sfWebRequest $request)
{
  $excel_file = Doctrine::getTable('Project')->find($request->getParameter('id'));

  $this->setLayout(false);
  sfConfig::set('sf_web_debug', false);

  $excelpath = sfConfig::get('sf_upload_dir').DIRECTORY_SEPARATOR.'project'.DIRECTORY_SEPARATOR.$excel_file['file'];

  // check if the file exists
  $this->forward404Unless(file_exists($excelpath));
  
  $content = file_get_contents($excelpath);
  
  $response = $this->getContext()->getResponse();
  $response->setHttpHeader('Pragma', '');
  $response->setHttpHeader('Cache-Control', '');
  $response->setHttpHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  $response->setHttpHeader('Content-Disposition', 'attachment; filename="invoice.xlsx"');
  $response->setContent($content);
  
  return sfView::NONE;
}

Note the file_get_contents.

i have tried that method but i get the same results.

… and the diff?