CodeIgniter force_download 未下载文件,而是将垃圾字符打印到控制台
CodeIgniter force_download not downloading file, but printing garbage characters to the console
我正在尝试从我的数据库中提取数据,写入 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) 但得到了相同的响应。
我错过了什么??
谢谢!
感谢评论中帮助我解决问题的所有人。我不再使用 ajax 调用,而是使用了一个表单。这是视图:
<form action='<?php echo site_url('data/downloadDataSubset');?>' method='post'><input type='hidden' name='projectIDs' id='projectIDs'>Download Standardized Datasets: <input type='hidden' name='getData' id='getData'><button class="btn btn-secondary" id='downloadDataSubset'>CSV</button></form>
然后我将控制器方法名称更改为 downloadDataSubset(并删除了末尾的 echo),它成功了!这是新的控制器方法(在 Data.php 控制器中)。
public function downloadDataSubset() {
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);
}
$data=file_get_contents($zipPath);
$this->load->helper('download');
force_download('search_results_data.zip',$data);
}
我正在尝试从我的数据库中提取数据,写入 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) 但得到了相同的响应。
我错过了什么??
谢谢!
感谢评论中帮助我解决问题的所有人。我不再使用 ajax 调用,而是使用了一个表单。这是视图:
<form action='<?php echo site_url('data/downloadDataSubset');?>' method='post'><input type='hidden' name='projectIDs' id='projectIDs'>Download Standardized Datasets: <input type='hidden' name='getData' id='getData'><button class="btn btn-secondary" id='downloadDataSubset'>CSV</button></form>
然后我将控制器方法名称更改为 downloadDataSubset(并删除了末尾的 echo),它成功了!这是新的控制器方法(在 Data.php 控制器中)。
public function downloadDataSubset() {
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);
}
$data=file_get_contents($zipPath);
$this->load->helper('download');
force_download('search_results_data.zip',$data);
}