Jump to content

Header function not workin in ObjectModel.php


Recommended Posts

I'm trying to export HTML to Excel data

 

I get the HTML and want to use header function in Object Model Core class function, below is my code

 

public function getRefrigHtmlToExcelReport($htmlTableResultSM){
        header("Content-type: application/ms-excel");
        header("Content-Disposition: attachment;filename=test.xls");
        header('Cache-Control: public');
        echo $htmlTableResultSM;
        exit();
    }

 

in browser console I get the "$htmlTableResultSM" but no excel file downloaded..Please help me, why header is not work in this class. If I test in a single php file then ot works.

 

Thanks

Edited by shovonbugs (see edit history)
Link to comment
Share on other sites

'Classes' are supposed to work as 'models' in the mvc pattern, I suggest you try with a new controller :) Although.... I am not quite sure why it's not working at the moment  :blink:

 

I've also tried into "AdminHomeController.php"  which is admin controller....but not response

Link to comment
Share on other sites

Try something like this

		header('Content-Transfer-Encoding: binary');
		header('Content-Type: '.$mimeType);
		header('Content-Length: '.filesize($file));
		header('Content-Disposition: attachment; filename="'.$filename.'"');
		$fp = fopen($file, 'rb');
		while (!feof($fp))
			echo fgets($fp, 16384);

		exit;

Replace variables, of course ;)

Link to comment
Share on other sites

Try something like this

		header('Content-Transfer-Encoding: binary');
		header('Content-Type: '.$mimeType);
		header('Content-Length: '.filesize($file));
		header('Content-Disposition: attachment; filename="'.$filename.'"');
		$fp = fopen($file, 'rb');
		while (!feof($fp))
			echo fgets($fp, 16384);

		exit;

Replace variables, of course ;)

 

I don't see any difference with my code

 

public function getRefrigHtmlToExcelReport($htmlTableResultSM){

        header("Content-type: application/ms-excel");

        header("Content-Disposition: attachment;filename=test.xls");

        header('Cache-Control: public');

        echo $htmlTableResultSM;

        exit();

    }

 

here is my code...I'm trying to focus on header function..header function not work.

Link to comment
Share on other sites

I don't see any difference with my code

 

public function getRefrigHtmlToExcelReport($htmlTableResultSM){

        header("Content-type: application/ms-excel");

        header("Content-Disposition: attachment;filename=test.xls");

        header('Cache-Control: public');

        echo $htmlTableResultSM;

        exit();

    }

 

here is my code...I'm trying to focus on header function..header function not work.

 

what I am realized that when I use header function in popup some contents are showing before header function...but I do not know how to solve..

Link to comment
Share on other sites

Check what headers are you actually sending to the browser (Web Developer Toolbar - Information - View response headers) - they are likely overwritten.

 

Also replace:

 

header("Content-Disposition: attachment;filename=test.xls");
with:

 

header('Content-Disposition: attachment;filename="test.xls"');
and add

 

header('Content-Transfer-Encoding: binary');
to the beginning as suggested before.
Link to comment
Share on other sites

×
×
  • Create New...