PHP_XLSXWriter icon indicating copy to clipboard operation
PHP_XLSXWriter copied to clipboard

How to download file in post method ?

Open yogeshsaroya opened this issue 5 years ago • 2 comments

@mk-j How to download file in post method without saving file at server?

yogeshsaroya avatar Jan 08 '20 16:01 yogeshsaroya

PHP_XLSX_Writer uses ZipArchive, which writes a temporary file to the server. So there is always a temporary file saved on the server at some point.

mk-j avatar Jan 22 '20 15:01 mk-j

I wrote this method to download file:

public function download(string $filename){
		$buffer = $this->writeToString();
		header('Content-Description: File Transfer');
		if (headers_sent())
			$this->Error('Some data has already been output to browser, can\'t send XLSX file');
		header('Content-Transfer-Encoding: binary');
		header('Cache-Control: public, must-revalidate, max-age=0');
		header('Pragma: public');
		header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
		header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
		header('Content-Type: application/force-download');
		header('Content-Type: application/octet-stream', false);
		header('Content-Type: application/download', false);
		header('Content-Type: application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', false);
		if (!isset($_SERVER['HTTP_ACCEPT_ENCODING']) OR empty($_SERVER['HTTP_ACCEPT_ENCODING'])) {
			// don't use length if server using compression
			header('Content-Length: '.strlen($buffer));
		}
		header('Content-disposition: attachment; filename="'.$filename.'"');
		print $buffer;		
	}

NitemareReal avatar Oct 06 '20 12:10 NitemareReal

@NitemareReal Thank you!

LrntL avatar Sep 30 '22 09:09 LrntL

您好,我已经收到您的信件,将尽快回复您。

sayid avatar Sep 30 '22 09:09 sayid