Symfony2 /多重上传

时间:2015-08-05 21:44:40

标签: symfony doctrine-orm upload

我已完成此操作:SF2 Handle File Uploads with Doctrin

现在我尝试多次上传,但找不到更新的教程。我发现的所有东西都像2岁以上,并且不起作用。

有人可以帮我解决这个问题吗?

我尝试将 $ path $ file 更改为数组,并添加 foreach($ this->文件作为$文件)上传()功能,但......错误....

这是我的代码:

实体: http://pastie.org/private/1qvkhcdhojm1n1orfzuja

控制器: http://pastie.org/private/ppcoezaoicdufg5dsa9dw

Twig: http://pastie.org/private/eclba9e84kzfbh96ecgya

PS:我在路径中需要这样的东西(在数据库中):“image1.jpeg,image2.jpeg,image3.jpeg,...” 对不起,但我对此很愚蠢,只是在学习..

谢谢

1 个答案:

答案 0 :(得分:0)

这里有一些伪示例代码,显示我如何访问多个上传的文件并为每个上传的文件保留实体

    $form = $this->createForm('doc',null);
    $request = $this->getRequest();
    if ('POST' === $request->getMethod()) {
        $form->handleRequest($request);
        if ($form->isValid()) {
            $data=$form->getData();

            try {
                $i=1;
                // upload each file
                foreach ($data['file'] as $item) {
                    $document = new Document();
                    $document->setFile($item);
                    // upload file 

                    $document->upload();

                    $document->setUploadedBy($user->getUserName());
                    // set thumbnail path
                    $em->persist($document);   

                    // generate images and set paths
                    $imageService->optiPathSingle($document);

                    $i++;
                }

                 return $this->redirect($this->generateUrl('gleisdreieck_core_index'));

            } catch (\Exception $e) {
                $form->addError(new FormError($e->getMessage()));                   
            }


        }
        return array(
            "form"=>$form->createView(),
            "tags"=>$tags
        );
    } else{
        return array(
            "form"=>$form->createView(),
            "tags"=>$tags
        );
    }

用于formbuilder:

class DocumentType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder->add('file', 'file', array(
            'label' => 'Attachments',
            'required' => true,
            'attr' => array (
                'accept' => 'image/*',
                'multiple' => 'multiple'
            )
        ))
        ->add('save', 'submit');
    }
相关问题