How to download csv using jquery in php -


i 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 downloadfileaction in 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 $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 fire function downloadmyfile, generate alert unknown language,language seems japan. not able download file using jquery post method. kindly me

your download should 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 contents of file, in case csv data. $contenttype content type, in case "application/vnd.ms-excel; charset=utf-16le". , $filename filename appears when start downloading, in case be: "order_".time().".csv". don't use die() , don't use echo. when call controller download starts automatically.

hope helps. :)


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -