Question

simple question I guess, but can't figure it out... Here's the problem:

In the front end, I export some user-selected data to the server to dynamically create a file:

$("#export-button").click(function() {

$.post("'.$postUrl.'",{variable:exportSelection},
    function(data) {
        console.log(data);
    }
);});

Then, after I receive the data and create/save the file on the server, I am doing the following in php:

if (file_exists($file)) {
    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;}

What am I supposed to do in the post callback function instead of printing on the console in order to get a download prompt?

UPDATE: OK - I just realised that my #export-button was an anchor tag with no href... Now when I point to the file on the server, the problem is that when I click, it follows the link prompts to save file etc., but it gets to the file BEFORE the "new" version is generated (so using the previous selection at each click)

Was it helpful?

Solution

OK - I figured it out. I just needed to disable the link from pointing anywhere and then do:

window.location = 'my_file_location.doc';

... inside the callback function

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top