在下面的代码示例中,我正在调用下载页面。但是,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/config.php
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
应用程序/config/routes.php
$route['default_controller'] = 'Sample';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['sample'] = 'sample/index';
$route['sample/download'] = 'sample/download';