我正在嘗試從我的資料庫中提取資料,撰寫 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) 但得到了相同的回應。
我錯過了什么??
謝謝!
uj5u.com熱心網友回復:
感謝評論中幫助我解決問題的每個人。我不再使用 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 (并在最后洗掉了回顯),它起作用了!這是新的控制器方法(在 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);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/440908.html
上一篇:如何解決問題::file_get_contents(http://localhost:8085/assets/data/test.txt):無法打開流:HTTP請求失敗
