上传文件Symfony2清空数据

时间:2015-10-19 21:42:11

标签: symfony file-upload

我尝试使用Doctrine处理文件上传,但是当我在Profiler中检查请求POST参数

[
  prix => 50, 
  description => test test, 
  city => 1, 
  nbrLivres => 5, 
  livre => [
    titre => titre du livre, 
    isbn => 1111111110, 
    dateEdition => [
      month => 1, 
      day => 1, 
      year => 2010
    ], 
    langue => francais, 
    auteur => wail, 
    nbrPages => 4, 
    etatLivre => 1, 
    typeOuvrage => 1, 
    genre => 1
  ], 
  save => , 
  _token => gw-x78-gqUWxu5suZ8thFrvTFwUjpbxAfhzrcX_PQgs
]

我找不到它没有发送的文件值。

我的实体类:

    class Entity{
    ....
    /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        public $path;

        public function getAbsolutePath()
        {
            return null === $this->path
                ? null
                : $this->getUploadRootDir().'/'.$this->path;
        }

        public function getWebPath()
        {
            return null === $this->path
                ? null
                : $this->getUploadDir().'/'.$this->path;
        }

        protected function getUploadRootDir()
        {
            // the absolute directory path where uploaded
            // documents should be saved
            return __DIR__.'/../../../../web/'.$this->getUploadDir();
        }

        protected function getUploadDir()
        {
            // get rid of the __DIR__ so it doesn't screw up
            // when displaying uploaded doc/image in the view.
            return 'uploads/documents';
        }

    /**
     * @Assert\File(maxSize="6000000")
     */
    private $file;

    /**
     * Sets file.
     *
     * @param UploadedFile $file
     */
    public function setFile(UploadedFile $file = null)
    {
        $this->file = $file;
    }

    /**
     * Get file.
     *
     * @return UploadedFile
     */
    public function getFile()
    {
        return $this->file;
    }
public function upload()
{
    // the file property can be empty if the field is not required
    if (null === $this->getFile()) {
        return;
    }

    // use the original file name here but you should
    // sanitize it at least to avoid any security issues

    // move takes the target directory and then the
    // target filename to move to
    $this->getFile()->move(
        $this->getUploadRootDir(),
        $this->getFile()->getClientOriginalName()
    );

    // set the path property to the filename where you've saved the file
    $this->path = $this->getFile()->getClientOriginalName();

    // clean up the file property as you won't need it anymore
    $this->file = null;
}

    }

我遵循symfony2文档中的教程,但没有成功,任何想法?

表格类:

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class LivreType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('titre')
            ->add('isbn')
            ->add('dateEdition', 'date')
            ->add('langue')
            ->add('auteur')
            ->add('imageFile', 'file')

        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Livre'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'appbundle_livre';
    }
}

在Twig文件中

{{ form_start(depot, {'action': path('persistbook'), 'method': 'POST'}) }}
{{ form_widget(depot) }}
{{ form_end(depot) }}

1 个答案:

答案 0 :(得分:1)

doc所示,我认为你应该替换

->add('imageFile', 'file')

通过

->add('file', 'file')

因为它是您用来检索function upload()

中上传文件的属性

不要忘记Lifecycle callbacks,否则,上传方法将不会被调用