我试图从我的数据库中提取数据,写CSV文件,压缩这些CSV文件,然后下载zip。
我可以轻松地将文件写入服务器,当我手动下载它们时,它们看起来很好,但是当我调用force_download时,什么都没有发生, AJAX 调用的响应中有垃圾。
下面是我的控制器代码:
public function ajax_getDataSubset() {
if (!$this->input->post()){
echo json_encode(array('status'=>'failed','errors'=>array('Access Mode'=>'Disallowed')));
exit();
}
$options = $this->input->post();
if (isset($options['state'])) {
$options['state']=explode(',',$options['state']);
}
if (isset($options['TSN'])) {
$options['TSN']=explode(',',$options['TSN']);
}
if (isset($options['progList'])) {
$options['progList']=explode(',',$options['progList']);
}
$programData = $this->extractor_model->get_program_data($options);
$plotData = $this->extractor_model->get_plot_data($options);
$treeData = $this->extractor_model->get_tree_data($options);
// make returned data into CSVs
$programDataCSV = $this->extractor_model->data_to_csv($programData['data'], true);
$plotDataCSV = $this->extractor_model->data_to_csv($plotData['data'], true);
$treeDataCSV = $this->extractor_model->data_to_csv($treeData['data'], true);
require_once(APPPATH.'libraries/pclzip/pclzip.lib.php');
$rand=random_string('alnum',16);
$created=FALSE;
while (!$created){
if(!is_dir($this->config->item('temp_path').$rand)) {
mkdir($this->config->item('temp_path').$rand,0755);
$dir=$this->config->item('temp_path').$rand;
$created=TRUE;
}
}
$programPath=$this->config->item('temp_path')."$rand/programs.csv";
$plotPath=$this->config->item('temp_path')."$rand/plots.csv";
$treePath=$this->config->item('temp_path')."$rand/trees.csv";
$zipPath=$this->config->item('temp_path')."$rand/search_results_data.zip";
$zip= new PclZip($zipPath);
if ($programData['resultCount']>0){
//open filestream for program data
$program_handle=fopen($programPath,'a');
if (!$program_handle){
show_error('could not open hand for program data');
}
fwrite($program_handle,$programDataCSV);
fclose($program_handle);
$zip->add($programPath,PCLZIP_OPT_REMOVE_PATH,$this->config->item('temp_path').$rand);
}
if ($plotData['resultCount']>0){
//open filestream for plot data
$plot_handle=fopen($plotPath,'a');
if (!$plot_handle){
show_error('could not open hand for plot data');
}
fwrite($plot_handle,$plotDataCSV);
fclose($plot_handle);
$zip->add($plotPath,PCLZIP_OPT_REMOVE_PATH,$this->config->item('temp_path').$rand);
}
if ($treeData['resultCount']>0){
//open filestream for tree data
$tree_handle=fopen($treePath,'a');
if (!$tree_handle){
show_error('could not open hand for tree data');
}
//Get each component, write it to a temp file, zip it up and then force download
fwrite($tree_handle,$treeDataCSV);
fclose($tree_handle);
$zip->add($treePath,PCLZIP_OPT_REMOVE_PATH,$this->config->item('temp_path').$rand);
}
ob_clean();
$data=file_get_contents($zipPath);
$this->load->helper('download');
force_download('search_results_data.zip',$data);
echo json_encode($plotData);
}
我还尝试了force_download($zipPath,NULL),但得到了相同的响应。
我错过了什么?
谢谢你,谢谢你
1条答案
按热度按时间qlzsbp2j1#
感谢评论中的每一个帮助我解决这个问题的人。我不再使用 AJAX 调用,而是使用了一个表单。下面是视图:
然后我将控制器方法名改为downloadDataSubset(并删除了末尾的echo),它成功了!下面是新的控制器方法(在Data.php控制器中)。