GEnGEr GEnGEr - 3 months ago 16
Twig Question

Symfony Download variable as String

on my sympfony webapp should simply search a database for the right file and present a download link/button to get the file. But i only have a variable with the content of the file i want to be downloadable. So i think i cant use href because i have no file just a string.

The search Bar in my index.html.twig like this


<div id="main" class="search">
<p>Please insert the corresponding mail to find the S/MIME-Zertificat</p>
<div class="form-container">
{% if smimekey == false %}
<form action="{{path('search')}}" method="get">
<input type="text" class="email" name="sbar" placeholder="enter email ..." />
<input type="submit" value="search" class="submit"/>
</form>
{% elseif smimekey=="NULL" %}
<form action="{{path('search')}}" method="get">
<input type="text" class="email" name="sbar" placeholder="No such Email or Zertifikat found" />
<input type="submit" value="search" class="submit"/>
</form>
{% elseif smimekey!="NULL" %}
<form action="{{path('search')}}" method="get">
<input type="text" class="email" name="sbar" placeholder="enter email ..." />
<input type="submit" value="search" class="submit"/>
######### DOWNLOAD BUTTON / link ###############
</form>
{% endif %}

</div>
</div>



So this index.html.twig has 3 possible designs


  1. first time visit when no smimekey is selected

  2. no smimekey was found

  3. and a smimekey was found and should be downloadable



my php file wich goes into the db and searches for the stuff looks like this:

/**
* @Route("/search", name="search")
*/
public function searchAction(Request $request)
{
$sbar=$_GET['sbar'];
if (filter_var($sbar, FILTER_VALIDATE_EMAIL)) {
$smimekey = $this->getDoctrine()
->getRepository('AppBundle:Zertifikat')
->findOneByEmail($sbar);
$smimekey=stream_get_contents($smimekey->getSmimekey());
);


}
else{$smimekey="";}


if (!$smimekey) {
$smimekey="NULL";
}
return $this->render('default/index.html.twig', array(
'email'=>$sbar,'smimekey'=>$smimekey, 'base_dir' => realpath($this->container->getParameter('kernel.root_dir').'/..'),
));
}


so now that i have the smimekey wich was saved as a Blob in the DB is now a simple string in the variable smimekey and i return it to my index.html.twig

I did not find any good methods to download a "generated" file with my variable as content.

Answer
/**
 * @Route("/dw", name="dw")
 */
public function downloadAction(Request $request)
{
            $sbar=$_GET['crt'];
            if (filter_var($sbar, FILTER_VALIDATE_EMAIL)) {
                    $smimekey = $this->getDoctrine()
                    ->getRepository('AppBundle:Zertifikat')
                    ->findOneByEmail($sbar);
            $email=$smimekey->getemail();
            $smimekey=stream_get_contents($smimekey->getSmimekey());
            $email=explode("@",$email);


            }
            else{$smimekey="";}

            header('Content-Description: File Transfer');
            header('Content-Type: application/x-x509-email-cert');
            header('Content-Disposition: attachment; filename='.$email[0].".crt" );
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');
            header('Content-Length: 500');
            ob_start();
            echo $smimekey;
            exit;
    return $response;
    }

This is how i fixed it i realy made a seperat Action make a download Funktion