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 ttttim
      I'm building an upload form for user to add images to a page. I would like for the user to change the image order after they add the images. Basically how the image field in the backend works. So users can drag and drop the image order. 
      The problem is that I don't know how to rearrange the data of my image field based on the order a user submits?
       
    • By cosmicsafari
      Hi all,
      I am working on a module which creates templates, fields & pages upon install.
      I have ran into an issue where I have a numerous fields which I would like to override their labels depending on the template they are used in.
      Is it possible to do this dynamically via API at the point where I am creating the field group and template using the API.
      The following is the code which I am using at the moment.
      foreach($templatesArray as $name => $cTemplate) { if(array_key_exists('fieldGroup',$cTemplate)) { $fg = new Fieldgroup(); $fg->name = $name . '-fg'; foreach ($cTemplate['fieldGroup'] as $field) { //possibly at this point? $fg->add($this->fields->get($field)); } $fg->save(); } $t = new Template(); $t->name = $name; $t->fieldgroup = $fg; $t->noChildren = 1; $t->setIcon($cTemplate['icon']); $t->save(); } Is this even possible?
    • By cosmicsafari
      Hi All,
      I am working on a module at the moment which in theory will create a few simple fields, templates and pages which will all be linked together.
      All is going well so far, I have it generating said fields, templates and pages, however I would like to also set the pages sort order and icon programmatically at this stage also.
      I have yet to see any documentation or any other threads where this has been brought up, is it even possible programmatically without interacting with the database directly?
      This is the snippet which is generating my pages on the fly at the moment.
      foreach($arrayOfPages as $name => $page) { $p = new Page(); $p->template = 'page'; $p->name = $page['URL']; $p->title = $name; $p->parent = wire('pages')->get('name='.$page['parent']); //is it possible to set the icon and sort order at this point, before saving? $p->save(); }  
    • By bmacnaughton
      I have a hook that creates a page for a subset of the pages on our site. It uses the saved page's name as part of the created page's name.
      The problem I am having is that my hook, attached to Pages::saved(), is being called even when the page save failed because of missing fields. Is there a way I can tell that the page save failed due to missing fields?
       
      Never mind - it does succeed; it just issues warnings about required fields.
       
    • By rolisx
      Hi everyone
      I've started to build a newsfeed for a customer, but I'm stuck...
      I have a news-repeater field including date, title, text and images. I have the link (showing date and title) to each news on the left (with foreach) and the details on the right (showing date, title, text and images also with foreach). I would love to only show the current news on the right. The latest news should be visible on the right. When I click on the link on the left side, it should change the news content on the right side.
      Does anyone have a simple solution for this? Here's how it looks like at the moment: http://rolspace.net/hd/energiegossau/uber-uns/news/
      Thank you!
      Roli