Download file link problem?

I created link where i can download .xlsx file but when i click on that link the download will not work ? Please help me to resolve my issue.

/////// .ctp code /////


Download Tempalte for Uploading <?= $this->Html->link(__('Click Here'),['controller'=>'Leads','action'=>'download'],['title'=>'Click Here']);?>

///////////// controller code /////////////////////

public function download(){
$file_path = WWW_ROOT.‘downloads’.DS.‘template_contractor_upload.xlsx’;
$this->response->withFile($file_path, array(
‘download’ => true,
‘name’ => ‘template_contractor_upload.xlsx’,
));
return $this->response;
}

  • List item

all function starting with with means that its immutabble meaning that it will not modify object and give you new object with modifications so you need to:

$this->response = $this->response->withFile($file_path, array(
    ‘download’ => true,
    ‘name’ => ‘template_contractor_upload.xlsx’,
));
2 Likes

it’s works, but the .xlsx file content is broken and encoded. why?

like

��_s�X��0����R�����ɑ��J4�������5����k��p�3�J��2��MJ�1+��@�V��%�9RG\J�!�a�>���Ie��:���xk:�[mċV��.7Zʘ�9^�Q�"�vP<����$T�O1�]��O��$

hard to tell, try going to file address directly and check if it has same problem

Directly open correctly, but diownload time not

Maybe use browser tools or wget to inspect the exact returned data? I wonder if maybe you have something somewhere that’s generating some little bit of output, which sends default headers, and changes how your browser interprets the results.

1 Like

I think this will work for you

$filename = "template_contractor_upload.xlsx";
				$filepath = "downloads/template_contractor_upload.xlsx";// path of file
				header ( "Pragma: public" );
				header ( "Expires: 0" );
				header ( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
				header ( "Cache-Control: public" );
				header ( "Content-Description: File Transfer" );
				header ( "Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" );
				header ( "Content-Disposition: attachment; filename=\"" . $filename . "\"" );
				header ( "Content-Transfer-Encoding: binary" );
				header ( "Content-Length: " . filesize ( $filepath ) );
				ob_end_flush ();
				@readfile ( $filepath );
1 Like

I think you are making a mistake here with encoding - content type. Not you… in general.

$file_path = WWW_ROOT.‘downloads’.DS.‘template_contractor_upload.xlsx’;

Since it is already in WWW_ROOT, it is publicly accessible. Just go for it, right?
But if you want to obfuscate the URL, then it is wise to have the files in APP folder or remotely on an object storage which requires authentication but… that is a different game altogether.

The encoded thing suggests it did not pass the right mime-type. Try

$mime = mime_content_type($file_path);
$this->response = $this->response->withHeader('Content-Type', $mime);
$this->response = $this->response->withFile($file_path, [
                                       'download' => true,
                                       'name' => 'Epic-Excelsheet-At-Your-Service.xlsx'
                               ]);
return $this->response;

I think it MAY NOT work.
Or maybe chain it.

$this->response = $this->response->withHeader('Content-Type', $mime)->withFile($file_path, ['download' => true, 'name' => 'urgh.xlsx']);
return $this->response;
1 Like

Thank you all your response but not solved my problems

I tried your code it’s works but same issue happen with your code.

code works but blank excel downloaded. I am set file path.

I was check that but not any error or warning.

I have tried this code and it’s working for me.
$filename = “file_example_XLSX_10.xlsx”;
$filepath = “file_example_XLSX_10.xlsx”;
header ( “Pragma: public” );
header ( “Expires: 0” );
header ( “Cache-Control: must-revalidate, post-check=0, pre-check=0” );
header ( “Cache-Control: public” );
header ( “Content-Description: File Transfer” );
header ( “Content-type: application/zip” );
header ( “Content-Disposition: attachment; filename=”" . $filename . “”" );
header ( “Content-Transfer-Encoding: binary” );
header ( "Content-Length: " . filesize ( $filepath ) );
ob_end_flush ();
@readfile ( $filepath );
Note: Please palce your downloadable file in webroot folder.

Can you elaborate the issue?

Perform tail on error.log and paste it here.

tail error.log

Thank You all for your great time. This problem solved. Issue with some path of the file which is not getting through function.

1 Like

Can you help me please?, I have the same problem when I want to download the file. What is the solution?

I’ve solved this problem.

In controller should to add:
$file = $this->Files->get($id);
$file_n = $file->file;
$file_p = $file->file_path;
//var_dump($file_p,$file_n);
//exit();
$response = $this->response->withFile(
WWW_ROOT . “files\file\$file_p\” . $file_n ,
[‘download’ => true, ‘name’ => $file_n]
);
return $response; // Return response and prevent render a view.