在Symfonys示例之后,我一直在尝试将文件上传到我的表单中:https://symfony.com/doc/current/controller/upload_file.html可悲的是,我似乎没有让它工作 . 当我尝试使用表单上传图像时,显示此错误 .

Catchable Fatal Error:传递给AppBundle \ Service \ FileUploader :: upload()的参数1必须是Symfony \ Component \ HttpFoundation \ File \ UploadedFile的实例,null给定,在/ var / www / html / test / src /中调用第156行的AppBundle / Controller / DefaultController.php并定义

我的控制器中有以下内容:

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Users;
use AppBundle\Service\FileUploader;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\FileType;

class DefaultController extends Controller
{ 
**
     * @Route("/bearbeiten/{id}", name="edit")
     */
     public function editAction($id, Request $request, FileUploader $fileUploader){
         //Daten aus der Datenbank mit $id
          $listen = $this->getDoctrine()
                ->getRepository('AppBundle:Users')
                ->find($id);
        //Variabeln vom passende Eintrag werden geholt und gesetzt
        $listen->setVorname($listen->getVorname());
        $listen->setNachname($listen->getNachname());
        $listen->setStrasse($listen->getStrasse());
        $listen->setOrt($listen->getOrt());
        $listen->setPLZ($listen->getPLZ());
        $listen->setBeschreibung($listen->getBeschreibung());
        $listen->setBild($listen->getBild());

        $users = new Users();
        //Formular wird erstellt
        $form = $this->createFormBuilder($listen)
                ->add('vorname', TextType::class, array('attr'=>array('class'=>'form-control', 'style'=>'margin-bottom:0.5cm; width:50%;')))
                ->add('nachname', TextType::class, array('attr'=>array('class'=>'form-control', 'style'=>'margin-bottom:0.5cm; width:50%;')))
                ->add('strasse', TextType::class, array('attr'=>array('class'=>'form-control', 'style'=>'margin-bottom:0.5cm; width:50%;')))
                ->add('ort', TextType::class, array('attr'=>array('class'=>'form-control', 'style'=>'margin-bottom:0.5cm; width:50%;')))
                ->add('plz', TextType::class, array('attr'=>array('class'=>'form-control', 'style'=>'margin-bottom:0.5cm; width:50%;')))
                ->add('beschreibung', TextType::class, array('attr'=>array('class'=>'form-control', 'style'=>'margin-bottom:0.5cm; width:50%;')))
                ->add('bild', FileType::class, array('label'=>'Bild (JPEG-Datei)', 'data_class'=>null))
                ->add('save', SubmitType::class, array('label'=>'Speichern', 'attr'=>array('class'=>'btn btn-primary')))
                ->add('home', SubmitType::class, array('label'=>'Zurück', 'attr'=>array('class'=>'btn btn-default')))
                ->getForm();

        $form->handleRequest($request);
        //Falls die Form valid ist....
        if($form->isSubmitted()){

            //Daten aus der Form in Variabeln sichern
             $vorname = $form['vorname']->getData();
             $nachname = $form['nachname']->getData();
             $strasse = $form['strasse']->getData();
             $ort = $form['ort']->getData();
             $plz = $form['plz']->getData();
             $beschreibung = $form['beschreibung']->getData();
             $file = $users->getBild();
             dump($file);
             $filename = $fileUploader->upload($file);
             $users->setBild($filename);

             //Doctrine aktivieren
             $em=$this->getDoctrine()->getManager();
             $listen = $em->getRepository('AppBundle:Users');
             //Führt den Befehl in der DB aus
             $em->flush();

              if ($form->get('home')->isClicked()){
                $textsa = 'Zurück geklickt'; 
                dump($textsa);
                return $this->redirectToRoute('homepage');
            }
             //return $this->redirectToRoute('homepage');
         }

        return $this->render('main/edit.html.twig', array('listen'=>$listen, 'form'=>$form->createView())); 
     }}

对于上传,我创建了一个名为FileUploader.php的服务

<?php
namespace AppBundle\Service;

use Symfony\Component\HttpFoundation\File\UploadedFile;


class FileUploader
{
     private $targetDir;
     public function __construct($targetDir) {
         $this->targetDir = $targetDir;
     }
     public function upload(UploadedFile $file) {
         $fileName = md5(uniqid()).'.'.$file->guessExtension();
         $file->move($this->getTargetDir(), $fileName);
         return $fileName;
     }
     public function getTargetDir() {
         return $this->targetDir;
     }
}

该服务在services.yml中定义:

AppBundle\Service\FileUploader:
        arguments:
           $targetDir: '%file_directory%'

变量file_directory在config.yml中定义:

parameters:
        locale: en
        file_directory: '/tmp'

我已经转储了$ file的值,并且在提交后它是null,所以函数上传没有't work because the file has no value. I'米真的卡住并感激任何建议 .