CodeIgniter Forums
Download thriugh ajax - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Download thriugh ajax (/showthread.php?tid=57629)



Download thriugh ajax - El Forum - 03-28-2013

[eluser]GabrieleMartino[/eluser]
Hello,

I was wondering if is possible to download throught an ajax call.

I have the call through ajax

Code:
$('#download_file').on ('click', function (){
   $.ajax({
                        dataType: "json" ,
                        url: base_url + "chat/download_file",
                        success: function(data){  
                         alert("ok");
                            
                        }
                    });
  });

and a controller than is made


Code:
public function download_file(){
  $this->load->helper('download');
  $path = './uploads/'.$this->session->userdata('chat_id').'/files';
  $path_complete = $path.'/';
                $handle = opendir($path_complete);
            
                $files = glob($path_complete.'*'); // get all file names
    foreach($files as $file){ // iterate files
      if(is_file($file))
        echo ($file);
        $data = file_get_contents($path_complete.$file); // Read the file's contents
        $name = $file;
   force_download($name, $data);
   echo json_encode("Success");
      }
}

The methods call the path controller but nothing happen on the screen.

Any idea!?


Download thriugh ajax - El Forum - 03-28-2013

[eluser]CroNiX[/eluser]
http://stackoverflow.com/questions/6668776/download-file-through-an-ajax-call-php