Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to use VichUploader for save a uploaded file in Action? #120

Closed
cipherchien opened this issue Feb 21, 2013 · 3 comments
Closed

Comments

@cipherchien
Copy link

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,

@ftassi
Copy link
Collaborator

ftassi commented Feb 21, 2013

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 ftassi closed this as completed Feb 21, 2013
@Baachi
Copy link
Contributor

Baachi commented Feb 21, 2013

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

@cipherchien
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants