在null

时间:2019-02-21 07:19:37

标签: javascript php symfony upload symfony4

它给出了我在标题上写的错误。我之前使用过这个完全相同的代码,但现在不工作。我想我没有得到“身份证”,但我不知道为什么。 如果我在控制器上取消注释这些注释行,它将打开照片添加窗口,并让我选择照片,但是单击提交后,它不会上传。  拜托,我真的需要您的帮助。有任何想法吗?我在这里的stackoverflow上看到了同样的错误,但是该解决方案对我不起作用

*****这是我的控制器*****

/**
     * @Route("/{id}/fotoekle", name="duyurular_fotoekle", methods={"GET","POST"})
     */
    public function fotoekle(Request $request, $id, Duyurular $duyurular): Response
    {
        $form = $this->createForm(DuyurularType::class, $duyurular);
        $form->handleRequest($request);
        echo ('sefa');
       // if ($form->isSubmitted() && $form->isValid()) {
            echo ('sefa2');
            $file = $request->files->get('image');
            $fileName = $this->generateUniqueFileName().'.'.$file->guessExtension();
  //          dump($request);
    //        die();
            // $file = $duyurular->getFotograf();
            // $file = $form->get('fotograf')->getData();

            try{
                $file->move(
                    $this->getParameter('fotograflar_directory'),
                    $fileName
                );
            }catch (FileException $e){
            }
            $duyurular->setFotograf($fileName);
            $this->getDoctrine()->getManager()->flush();
            return $this->redirectToRoute('duyurular_fotoekle', ['id' => $duyurular->getId(),]);
//        }
//        return $this->render('duyurular/fotoekle.html.twig', [
//            'duyurular' => $duyurular,
//            'form' => $form->createView(),
//            'id' => $duyurular->getId(),
//        ]);
    }

这是我的.html.twig文件/表单代码

{% block body %}
    <h1>{{ duyurular.baslik }}</h1>
    <form action="{{ path('duyurular_fotoekle',{'id':id}) }}" method="post" enctype="multipart/form-data">
        Select image to upload:
        <input name="duyuruid" value="{{ id }}" type="number" readonly>
        <input type="file" name="image" id="fileToUpload">
        <input type="submit" value="Upload Image" name="submit">
    </form>
    {% if duyurular.fotograf != null %}
       <img src="{{ asset('yuklenen') }}/fotograflar/{{ duyurular.fotograf }}" height="100">
    {% endif %}
{% endblock %}

1 个答案:

答案 0 :(得分:0)

我必须对控制器进行更改。我在iedit.html.twig中添加了另一个名为“ iedit”的函数,而我称之为“ duyurular_fotoekle”。如果您不明白我的意思,便会问。

/**
     * @Route("/{id}/fotoekle", name="duyurular_fotoekle", methods={"POST"})
     */
    public function fotoekle(Request $request, Duyurular $duyurular, $id): \Symfony\Component\HttpFoundation\RedirectResponse
    {
        $form = $this->createForm(DuyurularType::class, $duyurular);
        $form->handleRequest($request);
        echo ('sefa 2');
        $file = $request->files->get('image');
        // $file = $duyurular->getFotograf();
        // $file = $form->get('fotograf')->getData();
        $fileName = $this->generateUniqueFileName().'.'.$file->guessExtension();
        try{
            $file->move(
                $this->getParameter('fotograflar_directory'),
                $fileName
            );
        }catch (FileException $e){
        }
        $duyurular->setFotograf($fileName);
        $this->getDoctrine()->getManager()->flush();
        return $this->redirectToRoute('duyurular_iedit', ['id' => $duyurular->getId(),]);
    }

    /**
     * @Route("/{id}/iedit", name="duyurular_iedit", methods="GET|POST")
     */
    public function iedit(Request $request,$id, Duyurular $duyurular,DuyurularRepository $duyurularRepository): Response
    {
        $duyurulist = $duyurularRepository ->findAll();
        $duyuruname = $duyurularRepository ->findBy(['id'=> $duyurular->getId()]);
        $form = $this->createForm(DuyurularType::class, $duyurular);
        $form->handleRequest($request);

        echo ('sefa 0');
        $this->getDoctrine()->getManager()->flush();
        if ($form->isSubmitted()) {

            echo ('sefa 1');
            return $this->redirectToRoute('duyurular_index', ['id' => $duyurular->getId()]);
        }

        return $this->render('duyurular/iedit.html.twig', [
            'duyurular' => $duyurular,
            'id'=>$id,
            'duyurulist' => $duyurulist,
            'duyuruname' => $duyuruname,
            'form' => $form->createView(),
        ]);
    }