0

I can't download file.I am trying to download file from server through Ajax. I got success response in Ajax data and also file in response but file was not download what i do and how to fix this issue. The file reads successfully and also sever path get proper. Please help me.

This one is java script when i call function and get response 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 code and here i want to download file here and

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

Controller Ajax and PHP perform perfectly and read file but its not download

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);
    }

     }

How can I download the file.

RST
  • 3,899
  • 2
  • 20
  • 33
Pinal Zala
  • 13
  • 2
  • 7

2 Answers2

0

You try to download a file through ajax which is impossible (at least the way you did it)

Now you have two options

Option #1 - leave the JS behind (why do you even need JS here - an alert after downloading is useless imho)

your view should look like

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

and your controller

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);
}

Option #2 - is a bit more tricky, because it needs some adaption, i'm not gonna write that code for you, but instead i give you some links to study

Download a file by jQuery.Ajax

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

download file using an ajax request


Overall i'm not judging your code but you did a pretty bad job in order to use CI properly ;)

Community
  • 1
  • 1
Atural
  • 5,389
  • 5
  • 18
  • 35
0

If you want using method POST that this is my solution:

crsf enable:

<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 disable:

<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>

you must change path controller.

Arkadiusz G.
  • 1,024
  • 10
  • 24