It has been a while since I downloaded files with Symfony2, and it seems that everything has changed, follow the guide in How to handle file downloads using Doctrine but is outdated and does not work.
when i try to link form a get error
Catchable Fatal Error: Argument 1 passed to Entity\Portada::setFile() must be an instance of Symfony\Component\HttpFoundation\File\UploadedFile, string given, ...
this is my controller
public function uploadAction(Request $request) { $portada = new Portada(); $form = $this->buildUploadForm($portada); $form->bind($request); if ($form->isValid()) { $portada->upload(); } else { throw new \Exception("Hay un error en el formulario"); }
my essence
<?php namespace MyName\MyBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\Validator\Constraints as Assert; class Portada { private $file; public $path; public function setFile(UploadedFile $file = null) { $this->file = $file; } public function upload() { $this->path = $this->getFile()->getClientOriginalName(); $this->getFile()->move( $this->getUploadRootDir(), $this->path ); $this->file = null; } public function getFile() { return $this->file; } public function getAbsolutePath() { return null === $this->path ? null : $this->getUploadRootDir() . DIRECTORY_SEPARATOR . $this->path; } public function getWebPath() { return null === $this->path ? null : $this->getUploadDir() . DIRECTORY_SEPARATOR . $this->path; } protected function getUploadRootDir() { return __DIR__ . '/../../../../web/'. $this->getUploadDir(); } protected function getUploadDir() { return 'uploads/portada'; } }
rkmax source share