Harmen

Push file to download after AJAX post

Recommended Posts

Harmen    58

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
Harmen    58

@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

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 celfred
      Hello,
      Just a simple (I think) question which is in the title of my post.
      Roughly speaking : here's my code (made-up because my real function is so long... I don't want to post it all here )

      function updateScore($player, $task, $real = true) { [...] $player->score = $player->score+$task->score; if ($task->name == 'new-equipment') {   $new-eq = $pages->get("name=sword");   $player->equipment->add($new-eq); } if ($real == true) {   $player->save(); } }
      Everything works fine when I call updateScore($player, $task, true), but if I call updateScore($player, $task, false), scores are untouched, but the equipment gets added !
      It used to work fine on PW2.7 but my update to PW 3.0.62 seems to have broken this... Is there a simple explanation ? I keep reading my code over and over and this is driving me crazy...
      Thanks !
    • By zaib
      I've a page "blog" which have child pages (blog posts). Right now I've 11 pages (blog posts) and I'm fetching all the posts in my "blog" pages which displaying fine, Issue is only 10 results are showing this is how I'm fetching
      $entries = $pages->find("template=blog-entry"); foreach($entries as $entry){ <a href='{$entry->url}'>$entry->title</a> }  
    • By celfred
      Hello,
      I'm desperately trying to update my website to PW 3.0.62 and I'm facing issues to to module compatibility. I was stuck with Pages2Pdf which I managed to solve bu updating the module from Github, but now it's the Pages Web Service module... and this time, I don't know wht to do
      The Module is found there. But it is quite old and I can't find it in the modules catalogue... and my site is making quite a use of it (I can't think of a way to do otherwise, sorry...)
      After adding the FileCompiler=0 to the module pages, the error I'm stuck with is :
      Fatal error: Class 'WireData' not found in /home/celfred/PlanetAlert/site/modules/ServicePages/ServicePages.module on line 22
      and I have no idea on what to do... I must admit I'm not a programmer but a middle-school teacher... (for your information, here's the site I'm talking about : http://planetalert.tuxfamily.org ) but I'm struggling hard to solve the different issues I have to face and I'm wlling to understand things. I have just spent many hours trying to make 2.8 work on my localhost (and it seems ok ) but I'd like to switch to 3.x to prepare the future
      If anyone had the will to spend a few minutes to try and help me, I would greatly appreciate.
      Thanks in advance !
      If you need more information to understand my problem, feel free to ask.
    • By hellomoto
      I want to implement tables with Dynatable populated via ajax. Here's what I could muster:
      if($config->ajax) { $results = $pages->find($sanitizer->selector()); foreach($results as $r) $array[] = [ $r->title, $r->modified ]; header("Content-type: application/json"); echo json_encode($array); exit; } $content .= <<<EOT <table id="my-ajax-table"></table> <script> $("#my-ajax-table").dynatable({ dataset: { ajax: true, ajaxUrl: '.', ajaxOnLoad: true, records: [] } }); </script> EOT; But that's really nothing. Pro tips?
    • By pppws
      hey there,
      i'm quite new to processwire but i'm having a great experience using it!
      right now i hit a point where i can't help myself out with google/searchfunction.
       
      to sketch the basic functions of my page:
      visitors can enter their e-mail in a form. when doing this pw creates a new page, using the e-mail's md5 hash as a name. the url of the page is sent to the user. the user now can edit the newly created page and can fill out fields like: name and year of birth (thanks to the docs those two already work like a charm!) but there are other fields like "residencies", which are repeaterfields containing three fields: city (text), country (text), current (checkbox). at my current version i can populate a new repeater field easily by using this code:
      $location = $page->locations->getNew(); $location->location = Munich; $location->country = Germany; $location->current = 1; $location->save(); $page->locations->add($location); now i want to populate the new reapter fields from input fields (using the simple form api). which leads me to my questions:
       
      1. can i somehow group/merge input fields to one "repeater input fields" right now i have:
      // create a text input -> locations $field = $modules->get("InputfieldText"); $field->label = __('City'); $field->attr('id+name','location'); $field->required = 1; $form->append($field); // append the field to the form // create a text input -> country $field = $modules->get("InputfieldText"); $field->label = __('Country'); $field->attr('id+name','country'); $field->required = 1; $form->append($field); // append the field to the form // create a checkbox -> locations $field = $this->modules->get('InputfieldCheckbox'); $field->attr('name', 'location_current'); $field->attr('autocheck', 1); $field->attr('uncheckedValue', 0); $field->attr('checkedValue', 1); $field->attr('value', $this->fname); $form->append($field); // append the field to the form 2. my desired layout for the form looks like this:

      by hitting the + button a new line shows. i'm wondering what's the best practice here. can is somehow use the above mentioned code itself as a repeater, or do i have to create several (uniqe) input fields in advance and hide them afterwards (javascript)?
       
      any help is appreciated!
      thanks!