简体   繁体   中英

How to download csv using jquery in php

I am using following piece of code,

<script type="text/javascript">
function fireDownload(){
    var path= '<?php echo URL; ?>/downloadmyfile';
    $.post(path,function(data){
   alert(data);
    });
}
</script>

I have the downloadfileAction in my controller

public function downloadmyfileAction()
    {
                $this->_helper->viewRenderer->setNoRender();
                $this->_helper->getHelper('layout')->disableLayout();
                  .....       .....         ....
        $this->view->List=$myData; 
                ///CREATE CSV
                $myFile = "ORDER_" . time() . ".csv";
                header("Content-Disposition: attachment; filename=\"$myFile\"");
                header("Content-Type: application/vnd.ms-excel; charset=UTF-16LE");
                $out = fopen("php://output", 'w');
                $csvData = array('SlNo', 'Test1','Test2','Test3','Test4','Test5');
                $o = fputcsv($out, $csvData, ',', '"');
                $count = 1;
                foreach($myData as $key => $value)
                {
                $csvData = array();
                $csvData = array($count,$value['Test1'],$value['Test2'],$value['Test3'],$value['Test4'],$value['Test5']);
                $o = fputcsv($out, $csvData, ',', '"');
                $count++;
                }

                fclose($fh);

                // DOWNLOAD CSV
                echo $out;
                die();

    }

When i fire the function downloadmyfile, it will generate alert with unknown language,language seems japan. I am not able to download the file using jquery post method. Kindly help me

your download should be something like this:

    $this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();

    $this->getResponse()
        ->setHeader('Content-Type', $contentType)
        ->setHeader('Content-Disposition','attachment; filename="'.$filename . '";')
        ->setBody($data);

Where $data is the contents of your file, in your case csv data. $contentType would be some content type, in your case "application/vnd.ms-excel; charset=UTF-16LE". And finally $filename would be the filename that appears when you start downloading, in your case that would be: "ORDER_".time().".csv". Don't use die() and don't use echo. When you call your controller the download starts automatically.

Hope that helps. :)

finally i found by using,

function firedownload(){ 
    document.fname.action='<?php echo ADMIN_URL; ?>/downloadmyfile';
    document.fname.submit();
}

Also Changed line in downloadmyfileAction()

// DOWNLOAD CSV
                echo $out;
                exit;   //die();

But still I could not able to find the solution in jquery post method, because of time i moved to another solution

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM