spadeX
July 13, 2019, 10:59am
1
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;
}
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’,
));
1 Like
spadeX
July 13, 2019, 12:18pm
3
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
spadeX
July 14, 2019, 5:17am
5
Directly open correctly, but diownload time not
Zuluru
July 14, 2019, 8:32pm
6
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
spadeX
July 16, 2019, 5:52am
9
Thank you all your response but not solved my problems
spadeX
July 16, 2019, 5:53am
10
I tried your code it’s works but same issue happen with your code.
spadeX
July 16, 2019, 5:54am
11
code works but blank excel downloaded. I am set file path.
spadeX
July 16, 2019, 5:58am
12
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
spadeX
August 3, 2019, 5:51am
15
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.