在下面的代码示例中,我正在调用下载页面。但是,force_download不起作用。没有重定向,因为我将页面称为index.php/sample/download
,所以它保持在同一页面上。只有网址正在改变。但是没有触发下载。此外,我不能将该页面称为直接/sample/download
。虽然我为index.php(index_page)指定了所需的配置,但如果我没有将下载链接指定为/sample/index.php/sample/download
,则会返回404错误。
应用/控制器/ Sample.php
class Sample extends MY_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url_helper', 'download'));
$this->load->model('Sample_model');
}
public function index() {
$data = $this->getData();
$this->load->view('sample/view', $data);
}
public function download() {
$data = 'Here is some text!';
$name = 'mytext.txt';
force_download($name, $data);
}
}
应用/视图/样品/ view.php
<a href="<?php echo base_url(); ?>index.php/sample/download">Download file</a>
应用/配置/ config.php中
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
应用/配置/ routes.php文件
$route['default_controller'] = 'Sample';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['sample'] = 'sample/index';
$route['sample/download'] = 'sample/download';
答案 0 :(得分:0)
按照
更新您的代码<强>应用/控制器/ Sample.php 强>
class Sample extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url_helper', 'download'));
$this->load->model('Sample_model');
}
public function index() {
//$data = $this->getData(); // NOT DEFINED
$this->load->view('sample/view', $data);
}
public function download() {
$data = 'Here is some text!';
$name = 'mytext.txt';
force_download($name, $data);
}
}
<强>应用/视图/样品/ view.php 强>
<a href="<?= base_url('sample/download'); ?>">Download file</a>
<强>应用/配置/ routes.php文件强>
$route['default_controller'] = 'sample';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['sample'] = 'sample/index';
$route['sample/download'] = 'sample/download';
确保您的.htaccess
文件如下所示
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</IfModule>