Symfony3不会将文件转换为UploadedFile

时间:2017-02-06 23:48:19

标签: php symfony

我遵循本教程http://symfony.com/doc/current/controller/upload_file.html,但$ file未被转换为UploadedFile,它仍保留为字符串:

getTemp(): Observable<testing[]> {
    const url = this._Url+'/temperature';
    return this._http.get(url)
        .map(res => {
            let temp = res.json();
            //Add logic here that loops through temp and cleans value
            return temp;
            }).catch(this.handleError);
        }   

var_dump($file);
/home/owner/Desktop/Workspace/Documentary/src/DW/DocumentaryBundle/Controller/Admin/DocumentaryController.php:103:string '/tmp/phpmmv1LP' (length=14)

Type error: Argument 1 passed to DW\DocumentaryBundle\Uploader\PosterUploader::upload() must be an instance of Symfony\Component\HttpFoundation\File\UploadedFile, string given, called in /home/owner/Desktop/Workspace/DocumentaryWIRE/src/DW/DocumentaryBundle/Controller/Admin/DocumentaryController.php on line 106

/**
     * @param Request $request
     * @return Response
     */
    public function createAction(Request $request)
    {
        $documentary = new Documentary();

        $form = $this->createForm(DocumentaryType::class, $documentary);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $file = $documentary->getPoster();

            $posterUploader = $this->getPosterUploader();
            $fileName = $posterUploader->upload($file);

            $documentary->setPoster($fileName);

            $documentaryService = $this->getDocumentaryService();
            $documentaryService->save($documentary);
        }

        return $this->render('DocumentaryBundle:Admin:create.html.twig', array(
            'form' => $form->createView(),
        ));
    }

{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}

<?php

namespace DW\DocumentaryBundle\Form;

use DW\DocumentaryBundle\Entity\Documentary;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class DocumentaryType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title', TextType::class)
            ->add('storyline', TextareaType::class)
            ->add('summary', TextareaType::class)
            ->add('year', IntegerType::class)
            ->add('length', IntegerType::class)
            ->add('status', TextType::class)
            ->add('views', IntegerType::class)
            ->add('shortUrl', TextType::class)
            ->add('videoId', TextType::class)
            ->add('videoSource', TextType::class)
            ->add('poster', FileType::class, [
                'label' => 'Poster'])
            ->add('category', EntityType::class, [
                'class' => 'CategoryBundle:Category',
                'choice_label' => 'name',
            ])
            ->add('submit', SubmitType::class);
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => Documentary::class,
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return "documentary";
    }
}

3 个答案:

答案 0 :(得分:8)

文档错误!他们的示例中不使用类型提示:

// src/AppBundle/Entity/Product.php
public function setBrochure($brochure)
{
    $this->brochure = $brochure;

    return $this;
}

使用string类型提示时,将调用UploadedFile :: __ toString()! 所以检查你的实体。它应该是这样的:

public function setPoster($poster)
{
   // ...
}

答案 1 :(得分:0)

要从Symfony获取请求文件,您需要获取$ request-&gt;文件。例如$request->files->get('file_name');

答案 2 :(得分:0)

文档错误,应该是:$file = $form->get('poster')->getData();

/**
     * @param Request $request
     * @return Response
     */
    public function createAction(Request $request)
    {
        $documentary = new Documentary();

        $form = $this->createForm(DocumentaryType::class, $documentary);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $file = $form->get('poster')->getData();

            $posterUploader = $this->getPosterUploader();
            $fileName = $posterUploader->upload($file);

            $documentary->setPoster($fileName);

            $documentaryService = $this->getDocumentaryService();
            $documentaryService->save($documentary);
        }

        return $this->render('DocumentaryBundle:Admin:create.html.twig', [
            'form' => $form->createView(),
        ]);
    }