0

I am posting a HTML table data as json to server side using jquery $.post for writing the whole table data to csv file. but this is not outputting csv as downloadable for user.

I want to pop up the csv file (which normally happens when we download a file. you know the SAVE or OPEN box for csv)

Client side code

//selectedData is having the data as json 

$.post('ajax/csv_download.php', { 
    selectedData: JSON.stringify(selectedData) 
}, function(html){  });

Server Side code

   global $fh; 
   $fh = @fopen( 'php://output', 'w' );
   $post_data = json_decode($_POST['selectedData']);
   foreach ($post_data as $arr)
   {
     $val1 = $arr->val1  ;
     $val2 = $arr->val2 ;
     $val3 = $arr->val3 ;
     $val4 = $arr->val4 ;
     $val5 = $arr->val5 ;
      $out = array($val1,$val2,$val3,$val4,$val5);
       fputcsv($fh, $out);
      }
4

1 に答える 1

0

PHPで「Content-Type」を「text/plain」に設定した後、CSVファイルの内容をブラウザに書き戻したいようです。Web ブラウザーの構成方法に応じて、ユーザーはファイルを保存して開くように求められます。

<?php 
  $content="name,age,height,weight,gender"; 
  $file="persons.csv";

  header("Content-Type: text/plain"); 
  header("Content-disposition: attachment; filename=$file"); 
  header("Content-Transfer-Encoding: binary"); 
  header("Pragma: no-cache"); 
  header("Expires: 0");

  echo "$content";
?>
于 2013-04-24T03:40:25.983 に答える