Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to trigger file download to a user's browser?

Is it possible to force file download to user's browser (with the standard dialog prompt of course)? To be triggered by the server. Basically I'm thinking of pushing a file to the browser coming from server filesystem. Or is this not possible because of security sandbox? If this is not possible, is there a different way very close to this? I do not want to send the file w/o the user's consent. I can think of two step approach, first to prompt user of incoming data, and let user click on an OK button which triggers download, but near the end of download, user will get another confirmation box (the standard download prompt which asks to open or save). It would be nice if its possible with one step.

like image 808
StephenNYC Avatar asked Feb 04 '26 13:02

StephenNYC


1 Answers

Use an ajax json script that polls the server every 5 seconds or 10 seconds, when the server has a file ready responds with a positive answer "I have a file" have an iframe move to that url where the file is, and then it's handled as a normal download.

I have provided a little example of how I should think that it should work, so you have something to work from. I haven't checked it for bugs though, it's more a proof of concept thing.

jQuery example(without json):

$.ajax('/myserverscript.php?fileready=needtoknow').done(function(data) 
    {
    if(data.indexOf("I have a file") != -1)
        {
        xdata = data.split('\n');
        data = xdata[1];
        document.getElementById('myiframe').location.href = data;
        }
    });

PHP code

$x = filecheckingfunction();// returns false if none, returns url if there is a file
if($x !== false)
    {
    echo 'I have a file\n';
    echo $x;
    }
like image 87
Tschallacka Avatar answered Feb 06 '26 02:02

Tschallacka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!