I am required to download different type of files from a spesicif folder using ajax .The problem is that when execute the code I get this message this I have looked for the similar tutorials I coundnt find a solution Could you please help

This my index.php file

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class="table-responsive">
<div id="data"></div>
</div>
</body>
</html>

<script>
$(function(){
    fetchData();

    function fetchData(){
    $.ajax({
        url:"ajax.php",
        method:"post",
        success:function(data){
        $('#data').html(data);
        }
    })
    }

    $(document).on('click','.download',function(e){
        e.preventDefault();
var fileName=$(this).attr("id");
    $.ajax({
            url:"download.php",
            method:"post",
            data:{fileName:fileName},
            success:function(e){
                alert(e);
            }
        })

    });
})

this my ajax.php file that I am using populate the file Information table (like names, and download Count)

<?php

$handleDir = opendir($directory) or die('error');
$fileName = array();
$count = (count(scandir($directory)) - 2);
$output .= '<table class="table table-bordered">
<th width="20%">File Name</th>
<th width="10%">Download Count</th>
';
if ($count) {
    while ($file = readdir($handleDir)) {
        if ($file == '.' || $file == '..') {
            continue;
        }

        $fileName[] = $file;
    }

    $query = 'select * from download_manager order by  id desc';
    $fileInfo = array();
    $result = mysqli_query($link, $query);
    if (mysqli_num_rows($result)) {
        while ($singleFile = mysqli_fetch_array($result)) {
            $fileInfo[$singleFile['filename']] = $singleFile['downloads'];
        }
    }

    foreach ($fileName as $key => $value) {
        $output .= '<tr><td >
        <span class="download" id="'.$value.'">'.$value.'</span>
       </td>
       <td>'.$fileInfo[$value].'</td>
       </tr>';
    }
} 

$output .= '</table>';
echo $output;

?>

And this is my download php file error occurs in here

if (isset($_POST['fileName'])) {
    $fileName = $_POST['fileName'];

    if (file_exists($directory.'/'.$fileName)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.$directory.'/'.$fileName);
        header('Content-Length: '.filesize($directory.'/'.$fileName));
        ob_clean();
        flush();
        readfile($directory.'/'.$fileName);
        echo 'ok';
        exit;
    } else {
        echo 'no file';
    }
}

any help would be much appreciated Thank you

You can't use AJAX to force / suggest a browser to download a file. With AJAX you can send data and receive data in JS. What you could do is use AJAX (sending some parameters) to give the authority to a user to access a class / file in another tab to download the file. But would be a lot more straightforward just to use JS in order to create and post a form containing those data in another tab in a hidden iframe.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.