Symfony2上传文件代码

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\File\UploadedFile;
  
  
    /**
     * @Route("create", defaults={"name"="World"})
     * @Route("create/{name}", name="acme_test_form_create_post")
     */
    public function createAction($name)
    {
        $defaultData = array('message' => 'Type your message here');
        $form = $this->createFormBuilder($defaultData)
            ->setAction($this->generateUrl('acme_test_form_create'))
            ->setMethod('POST')
            ->add('table_file','file',array('label'=>'请选择上传文件:'))
            ->add('save','submit',array('label'=>'上传'))
            ->getForm();
        $request = $this->getRequest();
        $form->handleRequest($request);
        if ($form->isValid()) {
            // 获取csv文件并上传
            $data = $form->getData();
            $file = $data['table_file'];
            $file->move('upload/',rand().'.'.$file->getClientOriginalExtension());
              
            // 获取csv信息并更新数据库
              
        }
        return $this->render('AcmeTestBundle:Form:create.html.twig', array('form' => $form->createView()));
    }


by 雪洁 2014-09-13 03:38:39 3039 views
我来说几句

相关文章