doctrine2和Symfony2上传文件(csv的mimeTypes),获得正确的扩展名

时间:2015-08-28 11:05:16

标签: php symfony csv file-upload doctrine-orm

我正在使用symfony upload方法将csv或exel文件导入我的应用程序。

我的问题是,如果我不写这个mimeTypes "text/plain",symfony / doctrine就不会识别我的csv文件。

让我解释一下。这是我的实体代码:

namespace ...

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * Documents
 *
 * @ORM\Table(name="documents")
 * @ORM\Entity(repositoryClass="MySpace\MyBundle\Repository\DocumentsRepository")
 * @ORM\HasLifecycleCallbacks
 */
class Documents
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;
    /**
     * @var string
     *
     * @ORM\Column(name="doc_name", type="string", length=50, nullable=false)
     * @Assert\NotBlank
     */
    private $nomDocSuivis;
    /**
     * @var string
     *
     * @ORM\Column(name="doc_path", type="string", length=255, nullable=true)
     */
    private $path;
    /**
     * @var string $file
     * @Assert\File( maxSize = "100000000", mimeTypes= {"text/csv", "application/csv", "text/excel", "application/excel"}, mimeTypesMessage = "Please upload a valid CSV | exel file")
     */
    private $file;

    /*getters and setters*/
}

当然我使用简单的控制器方法来持久化和冲洗一个实体,这里没什么不对的。但是当我上传我的csv文件或exel文件时,我有mimeTypesMessage = "Please upload a valid CSV | exel file"。也就是说,symfony无法识别我的csv文件。

这是我的控制器代码:

public function uploadAction()
    {
      $em=$this->getDoctrine()->getManager();
      $doc = new Documents;

      $form=$this->createForm(new DocumentsType(), $doc);
      $form->remove('path');

      if ($this->getRequest()->isMethod('POST')) {
        $form->handleRequest($this->getRequest());

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();

            // An easy way to handle the file upload is to move it just before the entity is persisted and then set the path property accordingly
            // $docSuivi->upload();
            $doc = $form->get('file');
            $doc = $form->getData();
            $em->persist($doc);
            $em->flush();

            return $this->redirect($this->generateUrl('indexDocuments'));
        }
    }

     return $this->render('MySpaceMyBundle:Documents:uploadDocuments.html.twig', array('form' => $form->createView() ));
    }

最后我的Symfony表单生成器:

class DocumentsSuivisConsoType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('docName')
            ->add('path')
            ->add('file')
            ->add('compteurs')
        ;
    }

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

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

但是如果我添加mimeTypes =" text / plain"像这样:

    /**
     * @var string $file
     * @Assert\File( maxSize = "100000000", mimeTypes= {"text/plain", "text/csv", "application/csv", "text/excel", "application/excel"}, mimeTypesMessage = "Please upload a valid CSV | exel file")
     */
    private $file;

效果很好,但请将扩展名注册为.txt而不是.csv当然,我的.csv文件成为.txt文件,我不想要。

我哪里错了?

0 个答案:

没有答案