1. Go to this page and download the library: Download ethsam/symfony-dropzone library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ethsam / symfony-dropzone example snippets
public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder, array $options)
{
// userFiles is OneToMany
$builder->add('userFiles', DropzoneType::class, [
'class' => File::class,
'maxFiles' => 6,
'uploadHandler'=>'uploadHandler', // route name
'removeHandler'=> 'removeHandler'// route name
]),
->add('arrayIdMedia', TextType::class, ['mapped' => false]); //hide this type after tests
}
/**
* @Route("/uploadhandler", name="uploadHandler")
*/
public function uploadhandler(Request $request, ImageUploader $uploader) {
$dateNow = new \DateTime('now');
$doc = $uploader->upload($request->files->get('file'));
$file = new Attachment();
$file->setCreatedAt($dateNow);
$file->setUpdatedAt($dateNow);
$file->setImageFile($doc);
$this->entityManager->persist($file);
$this->entityManager->flush();
return new JsonResponse([ "id" => $file->getId() ]);
}
/**
* @Route("/removeHandler/{id}", name="removeHandler")
*/
public function removeHandler(Request $request, $id) {
$file = $this->repoAttachment->findOneBy(['id' => $id]);
$idFile = $file->getId();
$this->entityManager->remove($file);
$this->entityManager->flush();
return new JsonResponse([ "id" => $idFile ]);
}