Harmen

Push file to download after AJAX post

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

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 

  • 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 jploch
      Hi folks!
      For a website Iam working on I need to (pre)load a huge amount of images (100-500) from a folder in assets (wich I upload via FTP).
      To preload them I want to add them to the DOM inside a container, that I hide with css.
      This images will be use for a frame by frame animation (that animates with scrolling) so they should be loaded parallel and if the user clicks a cancel button, the loading should be canceled. (My website is using ajax to load pages with different animations, and the loading of the second animation waits till the loading of the first animation is loaded completly, wich I want to prevent). 

      I want to use ajax to do this, so I can cancel the loading with xhr.abort();
      Here is my code:
      var folder = '{$config->urls->assets}sequenzen/test/'; xhr = $.ajax({ url : folder, success: function (data) { $(data).find("a").attr("href", function (i, val) { if( val.match(/\.(jpe?g|png|gif)$/) ) { $(".preloader").append( "<img src='"+ folder + val +"'>" ); } }); } }); this will give me a 403 forbidden error.
      After some research I found out that I have to put a .htaccess in my assets folder.
      I also tried putting it in the sub folder "test", where the files are, but Iam still getting the error.

      Is there anything else Iam missing? Is there a configuration in PW i have to change to do that?
    • By celfred
      Hello,
      Still in my 'teaching game'. Here's my problem : I 'find' all players with a request like

      $allPlayers = $pages->find("template=player"); [/code Then, I limit to players belonging to the team of the logged in player with [code] $teamPlayers = $allPlayers->find("team=$loggedPlayer->team");
      No problem so far. But my scoreboards rely on either :

      $allPlayers->getItemKey($loggedPlayer);
      or

      $teamPlayers->getItemKey($loggedPlayer);
      to find the logged player's position in the charts.
      On the 'global' newboard with scoreboards based upon $allPlayers, everything works as expected.
      BUT on my 'team' newsboard, even though I'm using $teamPlayers, the returned indexes are based upon $allPlayers. Am I clear ? In other words, I have a total of 125 players, and my logged player is 61 out of 125 regarding the number of places he freed. But in his particular team of 25 players, he sould be 15 whereas he's still 61
      I'd like to reset my indexes (and start back from 0), but I can't find my way out of this...
      If someone has a hint to help, I'd appreciate.
      I have a second part in my worry : I had a way around it by simply making another 'raw' request :

      $teamPlayers = $pages->find("team=$loggedPlayer->team");
      Then my team indexes were right, but I faced another issue : Reordering my wirearray according to the scoreboard I want usually worked fine (simple sort() based upon an integer field, for example, player's coins, player's karma...) and indexes were updated BUT resorting with places.count ('places' field is a pageArray) doesn't update the indexes returned by getItemKey and my logged player is always at the position he was when I first did my initial $pages->find() query  
      So my way around found its limit and that's why I'm posting here, after struggling with this for a couple of hours...
      Thanks in advance for the help. 
    • By SwimToWin
      I am using ProcessWire as the back-end to manage contents on multiple sites (I call them "sub-domains"). I use ProcessWire as an editing system and database only. The ProcessWire templating system is not in use. I want to use ProcessWire's API and in particular the Include & Bootstrap method to output contents -- pretty much in the same way you do, if contents is stored in a MySQL database and output in PHP files that use PDO to load data.
      The issue is that I cannot get ProcessWire to fully work on each of the "sub-domains".
      https://processwire.com/api/include/ says:
       
      > You can do anything with ProcessWire's API that you can do from a template,
      > so all of this still applies, as does everything in the developer API.
      How can I get access to variables such as $input when using Include & Bootstrap?
      I tried these:
      $wire->pages->get("/foo/bar/"); echo "Segment1: " . $wire->input->urlSegment1; # Returns null $wire->pages->get("/foo/bar/"); echo "Segment1: " . $wire->input()->urlSegment1; # Internal Server Error $p = $wire->pages->get("/foo/bar/"); echo "Segment1: " . $p->input->urlSegment1; # Returns null $p = $wire->pages->get("/foo/bar/"); echo "Segment1: " . $p->input()->urlSegment1; # Returns null @ryan Can the API Variables documentation be updated with a section / an example that explain how to use "Include & Bootstrap" for each variable (since this is a really strong feature in ProcessWire)?
      $page $pages - Example: $homepage = $wire->pages->get("/"); $input $sanitizer $session $fields $user $log $templates $config
    • By jmartsch
      Hi folks, I published a new tutorial today which explains how to make a simple download function with ProcessWire (tested with version 3.0+). You find the tutorial on my website https://jensmartsch.de/blog/simple-file-downloads-with-processwire/
      Basically this is based on my post here in the forums 
       
    • By noelboss
      Maybe I'm missing something here, but I'm haveing a hard time to get absolute urls if PW is installed in a subdirectory thought the API...
      I need an absolute URL for the backend (as a redirect url of Oauth provider). To build the url i would normaly use
      url()->root.url()->admin – but this results in the install-path beeing present twice;
      domain.com/path/ + path/admin/
      In order to not use $_SERVER['HTTP_HOST'] I fetch the admin page from pages – but this is also not to easy since I need to remove the root folder from path as well. My current solution is somewhat complicated:
      $backendUrl = pages()->get('path="'.str_replace(urls()->root, '', urls()->admin).'", include=all')->httpUrl; The above code seems to be the savest way to get the absolute url to the backend using pure PW API, am I right?
      It would be great if one could fetch absolute urls with something like this
      $backendUrl = urls()->admin->httpUrl // or $backendUrl = httpUrls()->admin  
      SOLVED:
      Thank you @adrian – of course this HAD to be simpler:
      urls()->httpAdmin;