Vichuploaderbundle: Is it possible to use VichUploader for save a uploaded file in Action?

Created on 21 Feb 2013  ·  3Comments  ·  Source: dustin10/VichUploaderBundle

I know this is not VichUploader's goal, but I wish it is possible to be true, because VichUploader is too good to process file field and not only in database, but also in template!

the situation is I have a file already in server and I wish it can be save and relocate by VichUploader.

My code in action:

use Symfony\Component\HttpFoundation\File\File;
......
class documentController extends Controller {
......
        public function updateAction(Request $request){
                ......
                $form = $this->createForm(new DocumentType(), $doc);  
                ........
                if ($form->isValid()) {
                        $baseDir = $this->get('kernel')->getRootDir() . "/../web/uploads";  

                        $fileThumbnail = new File($baseDir."/image.jpg");  

                        $doc->setImgThumbnail($fileThumbnail->getFilename());

                        $sm = $this->get('vich_uploader.storage');
                        $sm->upload($doc);

                        $em->persist($doc);
                        $em->flush();
                        .......
                } //eof if
        }
}

I try to use VichUploader to save a uploaded file without form class, but it is not work with my way.

I wanna know is it possible save a uploaded file with VichUploader and without Form?

Thanks,

Most helpful comment

Thans @Baachi and @ftassi ,

@Baachi you are right!
It can be done with UploadedFile class. this is a hack ,but it is simple and effective.

the workable solution is

$fileThumbnail = new UploadedFile($baseDir.$filename, 'image.jpg', null, null, null, true);
$doc->setFileThumbnail($fileThumbnail);

$em->persist($doc);
$em->flush();

and everything is perfect.

All 3 comments

This won't work (I guess). In order to trigger the upload process you should have an UploadedFile binded to the entity and you're binding a File instance instead.

@ftassi A solution exists.
The UploadedFile class has a test parameter. If this parameter true, symfony will emulate a file upload.

Thans @Baachi and @ftassi ,

@Baachi you are right!
It can be done with UploadedFile class. this is a hack ,but it is simple and effective.

the workable solution is

$fileThumbnail = new UploadedFile($baseDir.$filename, 'image.jpg', null, null, null, true);
$doc->setFileThumbnail($fileThumbnail);

$em->persist($doc);
$em->flush();

and everything is perfect.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jcg678 picture jcg678  ·  6Comments

Propscode picture Propscode  ·  4Comments

nobady90 picture nobady90  ·  3Comments

webkmua picture webkmua  ·  4Comments

Marcelo-Petrucelli picture Marcelo-Petrucelli  ·  3Comments