Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
I am required to download different type of files from a spesicif folder using ajax .The problem is that when execute the code I I get this message

[ here]
I have looked for the similar tutorials I coundnt find a solution Could you please help

What I have tried:

This my index.php file
PHP
$(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
<?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
PHP
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
Posted
Updated 9-Oct-18 7:13am
v7
Comments
Richard MacCutchan 6-Oct-18 4:19am    
"The problem is that when execute the code I get this message".
And you think that we can guess what message and where it occurs?
Richard MacCutchan 6-Oct-18 7:57am    
You have updated the question, but we still have no idea what message or where it occurs.

1 solution

$.ajax({
    url:"download.php",
    method:"post",
    data:{fileName:fileName},
    success:function(e){
        alert(e);
    }
})

Your code is sending back the bytes of the file you want to download. You are then trying to display those bytes in an alert, which can only display text. That is why you are getting a message full of gibberish - that's Javascript trying its best to interpret the bytes as a string.

You can't use an AJAX request to download a file. Since you require a POST request, you'll need to create a hidden <form> pointing to your download page, and submit it from your code.

Alternatively, change your download.php code to work with a GET request, add the filename to the query-string on your link, and add the download attribute[^] to the link.
 
Share this answer
 
Comments
Member 3722539 11-Oct-18 15:43pm    
THANK YOU

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900