-1

I am rendering files details in html table by using below lines of code

for(var counterOfCerticateLoop in certificateDetailsArray)
{
   var document_type_id = certificateDetailsArray[counterOfCerticateLoop]['document_type_id'];
   var document_actual_name = certificateDetailsArray[counterOfCerticateLoop]['document_actual_name'];
   var document_unique_name = certificateDetailsArray[counterOfCerticateLoop]['document_unique_name'];
   var document_type = certificateDetailsArray[counterOfCerticateLoop]['document_type'];
   var document_description = certificateDetailsArray[counterOfCerticateLoop]['document_description'];
   var fileURL = '../student/certificates/'+document_unique_name;
   var EditButton = '<button title="Edit" onclick="editContactDetail(\''+document_type_id+'\')" class="btn btn-primary table_btn btn-outline btn-sm"><i class="fa fa-pencil"></i></button>';
   var DeleteButton = '<button title="Delete" onclick="deleteContactDetail(\''+document_type_id+'\')" class="btn btn-danger  table_btn btn-outline btn-sm"><i class="fa fa-trash"></i></button>';
   var DownloadButton = '<button title="Delete" onclick="download(\''+fileURL+'\')" class="btn btn-danger  table_btn btn-outline btn-sm"><i class="fa fa-download"></i></button>';

   var tableRow = '<tr><td>'+document_type+'</td>'+
           '<td>' + document_actual_name + '</td>'+
           '<td>'+document_description+'</td>'+
           '<td>'+DownloadButton+EditButton+DeleteButton+'</td>';
           $("#certificateTable").append(tableRow);
           }

  function download(url) 
  {    
       $.ajax({
                    url: '../common/downloadFile.php?path='+url,                       
                    type: 'post',
                    success: function(php_script_response){    
                           window.location = php_script_response;
                    }
            });
  }

All of these lines of code are written view file. All the information are rendered in html table perfectly

In downloadFile.php the code is like

       <?php

$path = $_REQUEST['path'];
if (file_exists($path))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>
Siva
  • 1,481
  • 1
  • 18
  • 29
Nida Amin
  • 735
  • 1
  • 8
  • 28

3 Answers3

0

I'm afraid it's not possible to download files via AJAX, you can find the explanation under this question: Download a file by jQuery.Ajax

But I don't see the reason why you want to use AJAX for this one.

You can do something like:

           var DownloadButton = '<a href="{domain}/download.php?id={fileId}" title="Download" class="btn btn-danger  table_btn btn-outline btn-sm" target="_blank"><i class="fa fa-download"></i></a>';

So basically, skip javascript and navigate the user directly to the script that will send the file to the user, with target="_blank" the file download will start in a new tab so it won't close or redirect the current window/tab.

Bakayaro
  • 120
  • 8
0

You need to create a link with attribute target="_blank", which refers to your download page (ex: 'http://example.com/common/downloadFile.php?path='+url)

When new window will opened and content downloaded, new window will closed (By default in modern browsers)

So, in your code, you can modify download function like this:

function download(url) { 
    var link = $('<a>', {
        href: '../common/downloadFile.php?path='+url,
        target: '_blank'
    });
    $(link)[0].click();
}
Veniamin
  • 459
  • 2
  • 9
0

I resoved it by following code

  function download(document_unique_name,document_actual_name) 
  {   
      window.location =  '../common/downloadFile.php?document_unique_name='+document_unique_name+'&document_actual_name='+document_actual_name; 
  }

It worked for me !!!

Nida Amin
  • 735
  • 1
  • 8
  • 28