【问题标题】:i can't download file force download not working how to fix and download file我无法下载文件强制下载不起作用如何修复和下载文件
【发布时间】:2016-11-07 08:01:10
【问题描述】:

我无法下载文件。我正在尝试通过 Ajax 从服务器下载文件。我在 Ajax 数据和响应文件中得到了成功响应,但文件没有下载我所做的以及如何解决这个问题。文件读取成功,服务器路径也正确。请帮我。

当我调用函数并获得响应时,这是一个 java 脚本 throw ajax

<script type="text/javascript">
    function push_file(files)
    { 
    $.ajax
            ({
                type: "post",
                url: "<?php echo base_url(); ?>Appointment/download_files/",
                data: "files=" + files,
               success: function(data)
                {
                   alert('ok' + data);
                }
            });
    }
    </script>

PHP 代码和这里我想在这里下载文件和

foreach($results as $row){
    $r_id = $row->id;
    <td><h5><a onclick="push_file(<?php echo $r_id;?>)"> Download </a></hs><t>
    }

控制器 Ajax 和 PHP 性能完美,可以读取文件但无法下载

public function download_files() {
       //$this->load->view ( 'ajax/download' );
       if($_POST)
    {

    $base = base_url();
    $id = $_POST['files'];
    $query = $this->db->query("select userfile from patient_report_file where        id='".$id."'");
    $result = $query ->row();
    $name = $result->userfile;
    echo $path = $base."Admin/uploads/patient_report/".$name; 
    force_download($path, NULL);
    }

     }

如何下​​载文件。

【问题讨论】:

    标签: javascript php ajax html codeigniter


    【解决方案1】:

    您尝试通过 ajax 下载文件,这是不可能的(至少是您这样做的方式)

    现在你有两个选择

    选项 #1 - 把 JS 抛在脑后(为什么你还需要 JS - 一个 下载后提醒没用恕我直言)

    你的视图应该是这样的

    foreach($results as $row)
    {
        echo '<td><h5><a href="'.base_url().'/Appointment/download_files/'.$row->id.'/"> Download </a></h5></td>';
    }
    

    和你的控制器

    public function download_files($id) 
    {
        //$this->load->view ( 'ajax/download' );
        $base = base_url();
        $id = $_POST['files'];
        $query = $this->db->query("select userfile from patient_report_file where        id='".$id."'");
        $result = $query ->row();
        $path = $base."Admin/uploads/patient_report/".$result->userfile; 
        force_download($path, NULL);
    }
    

    选项#2 - 有点棘手,因为它需要一些适应,我不会为你编写代码,而是给你一些学习链接

    Download a file by jQuery.Ajax

    https://github.com/eligrey/FileSaver.js/

    download file using an ajax request


    总的来说,我不会评判你的代码,但你在正确使用 CI 方面做得很糟糕;)

    【讨论】:

    • 选项 #1 是最好的解决方案,但我仍然无法下载文件空白页显示。
    【解决方案2】:

    如果您想使用 POST 方法,这是我的解决方案:

    crsf 启用:

    <script>
    function push_file(files)
        { 
        $('<form action="<?php echo site_url('admin/others/test');?>" method="post"><input type="hidden" name="files" value="'+files+'"><input type="hidden" name="csrf_token_name" value="<?php echo $this->input->cookie('csrf_cookie_name');?>"></form>').submit();
        }
        </script>
    

    crsf 禁用:

    <script>
    function push_file(files)
        { 
        $('<form action="<?php echo site_url('admin/others/test');?>" method="post"><input type="hidden" name="files" value="'+files+'"></form>').submit();
        }
        </script>
    

    您必须更改路径控制器。

    【讨论】:

      猜你喜欢
      • 2020-04-28
      • 1970-01-01
      • 2013-12-07
      • 1970-01-01
      • 2015-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多