在我的 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 消息?