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,
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.
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
and everything is perfect.