【发布时间】:2013-06-29 07:31:53
【问题描述】:
我有一个图片上传,只是注意到发生了两件事:
1) 表单在刷新时重新提交。显然不想那样。我找到了一个简单的 PHP 答案。我想知道 symfony 的做法是什么。
2) 上传文件后,我必须刷新才能看到该图像,这就是我注意到问题 1 的原因。
控制器代码:
public function displayThreadAction($thread_Id)
{
$em = $this->getDoctrine()->getManager();
$thread = $em->getRepository('GreenMonkeyDevGlassShopBundle:ForumThread')->find($thread_Id);
$post = new ForumReply();
$post->setThreadId($thread);
$form = $this->createForm(new ReplyImageForm(), $post);
$request = $this->getRequest();
if ($request->isMethod('POST')){
$form->bind($request);
if ($form->isValid()){
$image = new ForumReplyImage();
$image->setImageName($form['imageName']->getData());
$image->setImageFile($form['imageFile']->getData());
$image->upload();
$image->setReplyId($post);
$em->persist($post);
$em->persist($image);
$em->flush();
$post = new ForumReply();
$post->setThreadId($thread);
$form = $this->createForm(new ReplyImageForm(), $post);
}
}
return $this->render('GreenMonkeyDevGlassShopBundle:Forum:forum_thread.html.twig', array('thread' => $thread, 'form' => $form->createView()));
【问题讨论】:
标签: php image forms symfony upload