Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony2:使用flash消息提供文件下载服务_Symfony_Download_Flash Message - Fatal编程技术网

Symfony2:使用flash消息提供文件下载服务

Symfony2:使用flash消息提供文件下载服务,symfony,download,flash-message,Symfony,Download,Flash Message,在我的symfony2应用程序中,我允许用户下载一些文件: class FileController extends Controller { ... public function downloadFilesAction() { //This will serve a page listing all downloadable files and their url } public function downloadFi

在我的symfony2应用程序中,我允许用户下载一些文件:

class FileController extends Controller
{
     ...

     public function downloadFilesAction()
     {
        //This will serve a page listing all downloadable files and their url

     }

     public function downloadFileAction($file_id)
     {
         //This will be requested from the page served 
         //in the above "downloadFilesAction()"

         try {
             // Some processing goes here ...
         }
         catch (\Exception $e) 
         {
             $this->get('session')->getFlashBag()->add('error', 'Error occurred');
             return $this->forward('MyBundle:File:downloadFiles');
         }

         //get file content into &content variable

         $headers = array(
             'Content-Type' => 'text/csv;',
             'Content-Disposition' => "attachment; filename*=UTF-8''downloadedfile.csv",
         ); 

         return new Response($content, 200, $headers);
     }
}

让我们假设第一次出现错误并显示flash错误消息;第二次下载成功。问题在于第二次请求时,flash消息仍保留在页面上。下载成功后,如何清除flash消息?

只需先使用
downloadFileAction()
中的
get('error')
清除flash包的现有消息即可

请注意,使用
FlashBag->clear()
将取消设置所有消息(这是不需要的)


要重置闪存包,您可以重定向用户,而不是转发操作:

return $this->redirect($this->generateUrl('download_files_route'));

嗯,我复制并粘贴了您建议的代码,但它不起作用。问题是,当下载成功时,浏览器会显示文件下载对话框,而原始页面不会刷新。
return $this->redirect($this->generateUrl('download_files_route'));