用户
 找回密码
 入住 CI 中国社区
搜索
查看: 609|回复: 3
收起左侧

[版本 3.x] CI3.1.13 force_download 没反应,附代码

[复制链接]
发表于 2024-1-4 18:35:43 | 显示全部楼层 |阅读模式
本帖最后由 iginter 于 2024-1-4 19:00 编辑

  1. public function downloadtpl(){
  2.                 $filePath01='uploads/tpls/tpl.txt';
  3.                 if(file_exists($filePath01)){
  4.                         $data=file_get_contents($filePath01);
  5.                         echo $data;
  6.                         // $this->load->helper('download');
  7.                         // force_download('stockTpl.txt', $data);
  8.                         
  9.                 }else{
  10.                         show_error('bad file');
  11.                 }
  12.         }
复制代码
前端为
JS复制代码
 
$('#downfile').click(function(){
            $.post(' downloadtpl');
           
        });
 
 
复制代码


到echo $data这·一行运行是正常的,浏览器能显示文本中的内容;但把下面两句注释去掉后执行,没有任何反应,浏览器调试,发现执行也是200,搜索N多,不明白是哪里有问题,新入CI请指教
发表于 2024-1-5 22:32:22 | 显示全部楼层
PHP复制代码
public function downloadtpl()
{
    $filePath01 = 'uploads/tpls/tpl.txt';
 
    if (file_exists($filePath01)) {
        $data = file_get_contents($filePath01);
 
        $this->load->helper('download');
        force_download('stockTpl.txt', $data, 'application/octet-stream');
    } else {
        show_error('bad file');
    }
}
复制代码


试试这样可以吗?
 楼主| 发表于 2024-1-13 15:08:34 | 显示全部楼层
zidon 发表于 2024-1-5 22:32
试试这样可以吗?

感谢答复。
还是不行,我换直链下载了。
发表于 2024-2-23 17:22:09 | 显示全部楼层

js部分改成

本帖最后由 qq291561792 于 2024-2-23 17:23 编辑

            $.post(' downloadtpl');

改成

if ( $('#iframe_box').length == 0 ){
        var tmpdiv = document.createElement('div');
        tmpdiv.id = 'iframe_box';
        tmpdiv.style='width:1px; height:1px;';
        document.body.appendChild(tmpdiv);
}
var iframe = document.createElement('iframe');
iframe.src = "downloadtpl";
iframe.width = 1;
iframe.height = 1;
$("#iframe_box").find('iframe').remove();
$("#iframe_box").get(0).appendChild(iframe);

本版积分规则