【问题标题】:Symfony2 Ajax UploadSymfony2 Ajax 上传
【发布时间】:2013-03-04 23:16:39
【问题描述】:

我正在尝试使用 ajax 调用上传文件,但是当我去保存时,我只保存了与 anagrafic 的关系。 我声明,如果我尝试保存正常工作。

好像没有加载对象UploadFile! 我根据 symfony 食谱 http://symfony.com/doc/2.1/cookbook/doctrine/file_uploads.html 创建了表格 在我的控制器中

public function fileCreateAction($id)
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('MyBusinessBundle:Anagrafic')->find($id);
    $media = new Multimedia();
    $form = $this->createForm(new MultimediaType(), $media);

    if ($this->getRequest()->isMethod('POST')) {
        $form->bind($this->getRequest());
        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();

            $media->setAnagrafic($entity);
            $em->persist($media);
            $em->flush();

            $response = new Response();
        $output = array('success' => true);
        $response->headers->set('Content-Type', 'application/json');
        $response->setContent(json_encode($output));
        }
    }

我试图做一个 var_dump 的 var_dump($media);并返回:

object(My\BusinessBundle\Entity\Multimedia)[337]
  private 'id' => null
  private 'percorso' => null
  private 'alt' => null
  private 'type' => null
  public 'file' => null
  private 'anagrafic' => null

【问题讨论】:

    标签: ajax symfony symfony-2.1


    【解决方案1】:

    我不明白为什么.. 但是如果我使用插件 jquery https://github.com/blueimp/jQuery-File-Upload 来传递文件,我会得到正确的文件,我可以继续使用食谱的方法! 我的解决方案:

    public function fileCreateAction($id)
    {
        $em = $this->getDoctrine()->getManager();
    
        $entity = $em->getRepository('MyBusinessBundle:Anagrafic')->find($id);
        $media = new Multimedia();
        $form = $this->createForm(new MultimediaType(), $media);
    
        $request = $this->getRequest();
        if ($request->isMethod('POST')) {
            $form->bind($request);
            if ($form->isValid()) {
                $em = $this->getDoctrine()->getManager();
                $media->setAnagrafic($entity);
                $em->persist($media);
                $em->flush();
    
                if ($request->isXmlHttpRequest()) {
                    $response = new Response();
                    $output = array('success' => true);
                    $response->headers->set('Content-Type', 'application/json');
                    $response->setContent(json_encode($output));
    
                    return $response;
                } else {
                    return $this->redirect($this->generateUrl('user_img', array('id' => $entity->getId())));
                }
    
        } else {
        if ($request->isXmlHttpRequest()) {
            $errors = $form->get('file')->getErrors();
            $response = new Response();
            $output = array('success' => false, 'errors' => $errors[0]->getMessage());
            $response->headers->set('Content-Type', 'application/json');
            $response->setContent(json_encode($output));
    
            return $response;
        }
        }
        }
    

    考虑到双重调用 -> isXmlHttpRequest() 将进行重构,但这个概念有效! 我忘了..如果您将输入文件设置为“多个”,则会为每个文件发出请求!

    解决了!

    【讨论】:

      猜你喜欢
      • 2015-09-18
      • 1970-01-01
      • 2016-09-27
      • 1970-01-01
      • 2015-01-02
      • 2016-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多