/**
  * @Route("/upload/save/{admin_name}/{field_name}", name="sf_upload_save")
  * @Template()
  */
 public function imageAction(Request $request, $admin_name, $field_name)
 {
     $entity_id = (int) $request->get('id');
     $loader = $this->container->get('sf.admin.loader');
     if (!$loader->hasAdminName($admin_name)) {
         return $this->onError(sprintf('admin `%s` not exists(%d)!', $admin_name, __LINE__));
     }
     $admin = $loader->getAdminByName($admin_name);
     $config = $admin->getDoctrineConfigBy('file', $field_name);
     if (!$config) {
         return $this->onError(sprintf('admin `%s` file `%s` not exists(%d)!', $admin_name, $field_name, __LINE__));
     }
     $option = $admin->getFormOption($field_name);
     if (!$option) {
         return $this->onError(sprintf('admin `%s` file `%s` not exists(%d)!', $admin_name, $field_name, __LINE__));
     }
     /**
      * @var \Symfony\Component\HttpFoundation\File\UploadedFile
      */
     $handel = $request->files->get('attachment');
     if (!$handel || !$handel->isValid()) {
         return $this->onError('upload invalid');
     }
     $ext = strtolower(pathinfo($handel->getClientOriginalName(), PATHINFO_EXTENSION));
     if (!in_array($ext, $option['ext'])) {
         return $this->onError(sprintf('only allow (%s) you upload %s', join(', ', $option['ext']), $ext));
     }
     $session_id = $this->getRequest()->getSession()->getId();
     $file = new File();
     $file->setSessionId($session_id);
     $file->setName($handel->getClientOriginalName());
     $file->setExt($ext);
     $file->setSize($handel->getClientSize());
     $file->setType($handel->getMimeType());
     $file->setClassName($admin->getClassName());
     $file->setPropertyName($field_name);
     $file->setEntityId($entity_id);
     $stream = fopen($handel->getPathname(), 'rb');
     $data = stream_get_contents($stream);
     $file->setContent($data);
     $em = $this->get('doctrine')->getManager();
     $_file = $em->getRepository('Symforce\\AdminBundle\\Entity\\File')->loadByURL($request->get('url'));
     if ($_file && $admin->getClassName() === $_file->getClassName() && $field_name === $_file->getPropertyName() && $entity_id === $_file->getEntityId() && $session_id === $_file->getSessionId()) {
         $em->remove($_file);
     }
     $em->persist($file);
     $em->flush();
     return $this->sendJSON(array('url' => $this->getParameter('sf.web_assets_dir') . $file->__toString(), 'name' => $file->getName(), 'ext' => $ext, 'size' => $file->getSize()));
 }