【问题标题】:ContextErrorException in Symfony 3Symfony 3 中的 ContextErrorException
【发布时间】:2018-06-25 14:16:46
【问题描述】:

我正在尝试在 SF3 中上传图片,上传时出现此错误:

缺少 Symfony\Component\HttpFoundation\File\UploadedFile::__construct() 的参数 2。

这是我的实体中错误所在的部分(此处为第 9 行):

public function preUpload()
{
    // if there is no file (optional field)
    if (null === $this->image) {
        return;
    }

    // $file = new File($this->getUploadRootDir() . '/' . $this->image);
    $file = new File($this->getUploadRootDir() .'/' . $this->image);
    $uploadedfile = new UploadedFile($this->getUploadRootDir() .'/' . $this->image);

    // the name of the file is its id, one should just store also its extension
    // to make clean, we should rename this attribute to "extension" rather than "url" 
    $this->url = $file->guessExtension();

    // and we generate the alt attribute of the <img> tag,
    // the value of the file name on the user's PC
    $this->alt = $uploadedfile->getClientOriginalName();
} 

然后是我的控制器:

public function mediaEditAction(Request $request)
{
    $media = new Media();
    $form = $this->createForm(MediaType::class, $media);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $file = $media->getImage();
        $fileName = md5(uniqid()).'.'.$file->guessExtension();
        $file->move(
            $this->getParameter('images_directory'),
            $fileName
        );
        $media->setImage($fileName);

        $em = $this->getDoctrine()->getManager();
        $em->persist($media);
        $em->flush();

        $request->getSession()->getFlashBag()->add('Notice', 'Photo added with success');

        // redirection
        $url = $this->generateUrl('medecin_parametre');

        // permanent redirection with the status http 301
        return $this->redirect($url, 301);
    } else {
        return $this->render('DoctixMedecinBundle:Medecin:mediaedit.html.twig', array(
            'form' => $form->createView()
        ));
    }
}

【问题讨论】:

  • 错误信息似乎很清楚。 UploadedFile 的构造函数至少需要两个参数。你只是通过了一个。
  • 是的,我明白。但是我可以在这里添加的第二个参数应该是什么?

标签: image symfony upload


【解决方案1】:

您似乎在做不必要的工作,并且使这比可能需要的要复杂一些。你关注过这个Symfony guide for How to Upload Files吗?

与此同时,图像名称似乎是 $this-&gt;image 中的名称,因此您可以将其作为第二个构造函数参数传递。

$uploadedfile = new UploadedFile($this->getUploadRootDir().'/'.$this->image, $this->image);

但是,UploadedFile 可能只来自表单提交,而在您的实体中,您可能希望使用 File - 像这样:

use Symfony\Component\HttpFoundation\File\File;

$uploadedfile = new File($this->getUploadRootDir() .'/' . $this->image);

【讨论】:

  • 嗨 Jason Roman,你提到的第二个选项是我最初所做的,但它向我显示了以下错误:尝试调用类“Symfony\Component\HttpFoundation\ 的名为“getClientOriginalName”的未定义方法文件\文件”。当我尝试第一个解决方案时,出现此错误:类型错误:传递给 Doctix\MedecinBundle\Service\ImageUpload::upload() 的参数 1 必须是 Symfony\Component\HttpFoundation\File\UploadedFile 的实例,给定字符串,调用在 C:\wamp\www\Doctix-v2\src\Doctix\MedecinBundle\EventListener\ImageUploadListener.php 第 45 行
  • 对,您不能在常规文件上使用getOriginalName(),因为这不是上传的文件。我会尝试查看我为 Symfony 的文件上传指南发布的链接。看来您是一种混合行为 - 您的 preUpload Doctrine 听众对您所在的页面或您上传的文件没有内在的了解。
  • 是的,我理解 getclientoriginalname 的错误消息,这就是我选择另一个选项的原因。谢谢。我等你的回答。
  • 我没有什么要补充的了——你需要阅读我链接的 Symfony 文档以进一步帮助你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 2016-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多