Passing JavaScript ID Value to PHP for download

I am pretty new to PHP but I am learning fast.. I have been trying to use AJAX to Pass An ID to PHP. With this ID I use SQL to get the path of a file from the database than use it to download the file. However after some research this is not possible and I couldn't understand alternives... maybe there is a workaround?

These are my JavaScript function; I used JSON because I was passing an Array of IDs because I intend to use it in future for multiple downloads. I get the IDs from a table row click.

$('.btnDownload').click(function() {
                $.ajax({
                    url: 'scripts/downloadFile.php',
                    type: "POST",
                    data: {id: JSON.stringify(fileID)}
                });
            });

And my PHP to attempt to download the file;

<?php
    $data = $_POST['id'];
    $data = json_decode("$data", true);
    $countArray = count($data);
    $counter = 0;
while($countArray > $counter){
        $getID = $data[$counter];
// My sql connections and queries, jumped to the fetch part
$filePath = $fetch['filePath'];
                    header('Content-Type: application/zip');
                    header('Content-Disposition: attachment; filename='.basename($filePath));

                    readfile($filePath);
                    exit;
$counter++; 
    }
?>

How ever this script alone works when I give a static path and run it with my browser. I would like some help on alternatives on achieving the results.

Answers


To solve the problem relating to the popup not popping up, try something like this instead of ajax:

$('.btnDownload').click(function(event) {
    var url = 'scripts/downloadFile.php?id=' + JSON.stringify(fileID);
    window.location.href = url;
    event.preventDefault();
})

The user should be presented with a "save file" dialog and the browser will remain on the same url due to the disposition headers set by the php file.


The parameter that you're passing to downloadFile.php is called id, not ID (for example: data: {id: JSON.stringify(fileID)})

You need to update your PHP assignment to use the correct key as follows:

$data = $_POST['id'];

Mega.co.nz uses a similar approach. It requests the downloads per AJAX in order to allow a custom download progress bar. Once the download is finished, it is copied (technically it is downloaded from cache onto disk). You could do it somewhat like the following code. Of course, if you want to implement a custom progress bar, you'd need to fetch the unfinished response and update respectively.

$('.btnDownload').click(function(){
    $.ajax({
        url : 'scripts/downloadFile.php',
        type : 'POST',
        data : {id : JSON.stringify(fileID)}
    })
    .done(function( response ){
        location.href = 'data:,' + btoa(response);
    });
});

btoa converts the input into a Base64 encoded string which you can then use in conjunction with the Data URI Scheme to issue a download.


Need Your Help

Given an integer N, in how many ways can we tile a 4 x N rectangle with 3 x 1 tiles?

algorithm dynamic-programming

I know tiling problems are not uncommon and they are usually solved with Dynamic programming. I've also read quite similar question here about tiling 3XN rectangle with 2X1 tiles but I still have p...