phpsymfonyoneupuploaderbundle

OneupUploaderBundle: Files are not uploaded and UploadListener not persist to DB, why?


I'm trying to upload one or more files using OneupUploaderBundle but I can not get it to work since files aren't uploaded and nothing is persisted to DB. I'll explain what I've done til now:

config.yml:

oneup_uploader:
    mappings:
        recaudos:
            frontend: blueimp
            storage:
                service:              ~
                type:                 filesystem
                filesystem:           ~
                directory:            %kernel.root_dir%/../web/uploads/recaudos
                stream_wrapper:       ~
                sync_buffer_size:     100K
            allowed_mimetypes:     [application/msword,image/jpeg,image/pjpeg,image/png,application/pdf,application/vnd.oasis.opendocument.text]
            #disallowed_mimetypes:  []
            error_handler:        oneup_uploader.error_handler.noop

            # Set max_size to -1 for gracefully downgrade this number to the systems max upload size.
            #max_size:             9223372036854775807
            use_orphanage:        true
            enable_progress:      true
            enable_cancelation:   true
            namer:                oneup_uploader.namer.uniqid

At Twig template only this:

<input id="fileUpload3" class="fileUpload" type="file" name="fileUpload3[]" data-url="{{ oneup_uploader_endpoint('recaudos') }}" multiple />

$(document).ready(function(){
    $('fileUpload3').fileupload();
});

And latest this is the Listener for onUpload event:

namespace AppBundle\EventListener;

use Oneup\UploaderBundle\Event\PostPersistEvent;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity;

class UploadListener
{
    protected $doctrine;

    public function __construct($doctrine)
    {
        $this->doctrine = $doctrine;
    }

    public function onUpload(PostPersistEvent $event, Request $request)
    {
        $session = $request->getSession();
        $em = $this->doctrine->getManager();
        $request = $event->getRequest();
        $tempfile = $event->getFile();

        $productoSolicitud = $em->getRepository('SolicitudProducto')->find($session->get('productoSolicitudId'));

        $recaudosTramiteProductoSolicitud = new Entity\RecaudosTramitesProductoSolicitud();
        $recaudosTramiteProductoSolicitud->setProductoSolicitud($productoSolicitud);

        $filenamePart = explode("--", $tempfile->getName());
        $pathinfo = pathinfo($tempfile->getName());
        $recaudosTramiteProductoSolicitud->setArchivo($pathinfo['filename']);

        $em->persist($recaudosTramiteProductoSolicitud);
        $em->flush();
    }
}

And of course I have service defined also:

appbundle.upload_listener:
    class: "AppBundle\EventListener\UploadListener"
    arguments:  [@doctrine]
    tags:
        - { name: 'kernel.event_listener', event: "oneup_uploader.post_persist", method: "onUpload" }

Did I miss something? Why file are not uploaded and data is not persisted? As you may see in the attached image there is a post request to _uploader/recaudos/upload, any advice?

enter image description here


Solution

  • I just checked my own OneupUploader implementation:

    Rename your function "onUpload" to "onPostUpload" and change your service configuration to this:

    appbundle.upload_listener:
    class: "AppBundle\EventListener\UploadListener"
    arguments:  [@doctrine]
    tags:
        - { name: 'kernel.event_listener', event: oneup_uploader.post_upload, method: onPostUpload }
    

    Should work, works for me.

    If you need your original file name you also need an onUpload Method and this:

    tags:
        # - {...
        - { name: kernel.event_listener, event: oneup_uploader.pre_upload, method: onUpload }
    

    Regarding your comment:

    //UploadListener

    class UploadListener
    {
        protected $originalName;
    
        public function onUpload(PreUploadEvent $event)
        {
            $file = $event->getFile();
            $this->originalName = $file->getClientOriginalName();
        }
    
        public function onPostUpload(PostUploadEvent $event)
        {
            $fileEntity = new YourFileEntity();
            $fileEntity->setYourOriginalNameProperty($this->originalName);
            //...
        }
    
    }