I'm using liuggio/ExcelBundle, this is my code to write and download the xlsx:
$writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel2007');
$response = $this->get('phpexcel')->createStreamedResponse($writer);
$dispositionHeader = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
'XXX_-_' . ( new \DateTime() )->format( 'Y_n_j' ) . '.xlsx'
);
$response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8');
$response->headers->set('Cache-Control', 'maxage=1');
$response->headers->set('Content-Disposition', $dispositionHeader);
When I call the method directly from a React component it download ok:
e.preventDefault();
location.href = path.url_descarga_excel;
$.UIkit.init();
But I need to pass some arguments. When I try to download with AJAX:
e.preventDefault();
var sist_electrico = {
sic: ReactDom.findDOMNode(this.refs.SIC).checked,
sing: ReactDom.findDOMNode(this.refs.SING).checked
};
var periodo = {
inicio: this.refs.fechaInicio.getValue(),
fin: this.refs.fechaFin.getValue()
};
var form = $('input[name="radio_tipoForm"]:checked').val();
$.ajax({
url:path.url_descarga_excel, async:false,
type:"post", dataType:"json", data:{form, sist_electrico, periodo},
});
it doens't download, it just show the characters of the file in the preview of Chrome:
Preview:
PK�QI%���a[Content_Types].xml͔]K�0���%��f� "�v��R... etc
Headers:
Request URL:http://XXX/app_dev.php/reporte/descargaExcel
Request Method:POST
Status Code:200 OK
Response Headers
view source
Cache-Control:maxage=1, private
Connection:Keep-Alive
Content-Disposition:attachment; filename="XXX.xlsx"
Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8
Date:Thu, 11 Aug 2016 14:15:50 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.4.6 (CentOS) mod_fcgid/2.3.9 PHP/5.4.45
Transfer-Encoding:chunked
X-Debug-Token:ea66a8
X-Debug-Token-Link:/app_dev.php/_profiler/ea66a8
X-Powered-By:PHP/5.4.45
Request Headers
view source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate
Accept-Language:es-ES,es;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Content-Length:108
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:PHPSESSID=m6tcoofak8ihe02nit0nj7lj85
Host:cnecontrato.adevcom.cl
Origin:XXX
Pragma:no-cache
Referer:http://XXX/app_dev.php
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36
X-Requested-With:XMLHttpRequest
What I'm doing wrong?
Well, finally I just did this:
exportarExcel:function(e){
e.preventDefault();
var sist_electrico = {
sic: ReactDom.findDOMNode(this.refs.SIC).checked,
sing: ReactDom.findDOMNode(this.refs.SING).checked
};
var periodo = {
inicio: this.refs.fechaInicio.getValue(),
fin: this.refs.fechaFin.getValue()
};
var form = $('input[name="radio_tipoForm"]:checked').val();
location.href = path.url_descarga_excel + "?form=" + form + "&sist_electrico=" + sist_electrico + "&periodo=" + periodo;
$.UIkit.init();
the old location.href with parameters. Now the PHP is receiving the parameters and the xlsx is being downloaded correctly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.