Jump to content

Push file to download after AJAX post


Harmen
 Share

Recommended Posts

Hi folks,

 

On the website I am working on I do have a form which needs to be filled in (duh) and when the user clicks on download, the data is send with ajax and processed in the same file by using the ProcessWire API with $config->ajax;

The data which is provided by the user will be checked with data in my database. If the data is correct I want to push a file download. But this is not working at the moment. It seems like the content of the (.exe) file is pasted in the console instead of pushing the file. My code looks like this:

if ($config->ajax){  
    $serial = $input->post('serial');
    $DB = DB();
    $query = $DB->prepare("/*My query here*/");
    $query->bindParam("comparision", $serial, PDO::PARAM_STR);
    $query->execute();

    $reply = '';

    $SN_rows = $query->rowCount();
    if (!$SN_rows > 0) {
        $reply = 'error';
    } else {
        $reply = 'success';
    $installer = $page->attachments->eq(1);
    $filename = "Filename";
    $filepath = $installer->filename;

    $options = array(
        'forceDownload' => true,
        'exit' => true,
        'downloadFilename' => $filename
    );

    wireSendFile($filepath, $options);
    }

    echo $reply;
    exit();
}

What am I doing wrong here or is it a bug? Is there another way to do this with the processwire API?

 

Thanks in advance,

 

~Harmen

Link to comment
Share on other sites

@Robin S, thanks for the links. I fixed or cheated it in a bit different way. In the success function of the AJAX Post, I check for the reply if it is success or not. If it is equal to success I redirect to the same page with a query behind the normal URL. In the file I check if the query is in the URL and if so I push the file as a download. The only thing insecure to this method is that if you know the query you can still get the file without a serialnumber. Anyway, it works for the time being, hopefully this helps someone else 

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...