DTH
DTH

Reputation: 1133

Redirect after file download

My problem is:

Pressing a link on my page will (through the referred php file) call a function which downloads a file from my webservice. This means that header information is send upon file download, which also means that I'm not able to call the php header function to redirect BACK to my initial page where I clicked the link in the beginning. The reason WHY I want togo back to that page is that it will update my history of latest downloaded files, but not before the file is downloaded.

What is the BEST way to overcome this obstacle?

My function which initiates the download:

public function downloadAuthorityRules($api_key,$id)
{

    $curl_post_data = array('id' => $id); 

    $headers = array('Content-type: application/json','Authorization: '.$api_key,);
    $fp = fopen (dirname(__FILE__) . '/localfile.tmp', 'w+');//This is the file where we save the    information
    $curl = curl_init(DOWNLOAD_AUTHORITYRULES);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($curl_post_data));
    curl_setopt($curl, CURLOPT_USERPWD, $api_key);
    $file = curl_exec($curl);

    if ($file === false) 
    {
        $info = curl_getinfo($curl);
        curl_close($curl);
        die('error occured during curl exec. Additioanl info: ' . var_export($info));
    }

    curl_close($curl);

    header('Content-type: ' . 'application/octet-stream');
    header('Content-Disposition: ' . 'attachment; filename=myndighedsregler.pdf');
    echo $file;        
}

Upvotes: 0

Views: 2416

Answers (2)

DTH
DTH

Reputation: 1133

Found another way of solving my redirect problem, which could be of use to others:

My webpage currently uses AJAX to load content into my DIV'S, thus i already have a javascript function called loadPage.

What i did, was to implement a function called delayedLoadPage, which simply uses setTimeout as follows

function delayedLoadPage(page)
{
    setTimeout(function() { loadPage(page);},5000);     
}

This function will simply call the loadPage after 5 seconds have past.

I changed my where my reference to the php which does the download is located and added an onclick event

<a onclick="javascript:delayedLoadPage('authorityRules.php')" href="<? echo "downloadAuthorityRules.php?id=".$auth_current['auth_rules'][0]['id'];?>" >

The result is, that the file will start to download, and 5 seconds later my page is reloaded when the timeout occures and loadPage is called.

Upvotes: 1

developerwjk
developerwjk

Reputation: 8659

You cannot redirect from the download page after download completes. Its just not possible.

You will need to update the db table in the download page before you begin serving the file. Then your homepage that displays the download history will update naturally when its refreshed next (assuming you set no-cache headers properly).

Yes, I know, you want to force it to update. But you can't do it the way you were thinking.

What is the BEST way to overcome this obstacle?

You could do Ajax polling from the homepage to refresh the insides of a certain DIV every so many seconds. You will need to make sure to set cache: false in your $.ajax() call (if using jQuery, which I would recommend you do for Ajax). Otherwise, the info on the homepage might not pull the new data due to the browser caching Ajax results.

Upvotes: 1

Related Questions