Harmen

Push file to download after AJAX post

3 posts in this topic

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

Share this post


Link to post
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 

2 people like this

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By benbyf
      Hello,
      Does anyone know whether the is a place detailing the structure your admin theme should take when creating one? e.g. I see UiKit, Reno and Default admin themes all have default.php, init.php, controller.php files, is this a stylisitic choice or a prerequisite for any admin theme module?
      Thanks!
    • By bmacnaughton
      I am using the translation function (either $this->_() or __()) within a module that responds to AJAX API calls - there isn't really a page that is being served.
      When I supply a string with an apostrophe, e.g.,
      __('Book \'em danno') It is formatted as
      Book 'em danno  
      Is there some way to prevent output formatting when retrieving strings using the translation functions?
    • By dweeda
      At the top of each of my main pages, I want to redirect to my login page if the session is timed out.  How can I use the PW API to test for this?
      $user->isLoggedin() doesn't seem to do it, like I expected.
      THX
    • By Sipho
      I wish to access a template's allowed templates for it's children from the API. This one:

      I am pretty sure this is possible, but I can't seem to find it anywhere in the documentation. If this is not possible, is there some way of assigning properties to templates? So like fields that already have a value on all pages using that template.
    • By louisstephens
      I have a bit of a head scratcher for myself. I have a form (first name, last name, and domain) that I use to create pages. I have the first name and last name being combined and saving as the title, and being saved in their individual fields as well. The domain name is just being saved in it's designated field under the page. This all works quite well in fact.
      (just a note: for a special case, I am combining all three values so it looks like johndoe@domainselected.com)
      However, I thought I might need to check to see if the page already existed, and if it does, provide the user with an error. If it doesn't already exist, follow through with creating the page. I didnt want to save the entire address in the title as I knew "@" wouldn't fly with the url to the page.
      I thought I could use a foreach, but I understand that outputs an array, when I need to check just one entry.
      Does anyone have any suggestions? I am probably making this much more difficult than it needs to be.
       
        I have fixed the issue... "==" seemed to fix it.