dongmi1995 2016-09-01 15:28
浏览 21
已采纳

Symfony将变量下载为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.

  • 写回答

1条回答 默认 最新

  • dsfsdfsdfsdfsdf45454 2016-09-02 13:29
    关注
    /**
     * @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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作