簡體   English   中英

Symfony和PHPExcel(帶有React.js),未下載

[英]Symfony and PHPExcel (with React.js), not downloading

我正在使用liuggio / ExcelBundle,這是我編寫和下載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);

當我直接從React組件調用該方法時,它會下載確定:

    e.preventDefault();
    location.href = path.url_descarga_excel;
    $.UIkit.init();

但是我需要傳遞一些論點。 當我嘗試使用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},
    });

它不會下載,只是在Chrome的預覽中顯示文件的字符:

預習:

    PK�QI%���a[Content_Types].xml͔]K�0���%��f� "�v��R... etc

標頭:

    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

我做錯了什么?

好吧,終於我做到了:

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();

帶參數的舊location.href。 現在,PHP正在接收參數,並且已正確下載xlsx。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM