symfony 2上传一个捆绑包

时间:2013-11-28 12:59:15

标签: symfony file-upload upload event-handling oneupuploaderbundle

我使用OneUp包用于symfony 2,方法的监听器类永远不会调用

//service.yml
amd_picture.uploadListener:
    class: Amd\PictureBundle\Services\UploadListener
    arguments: ["@doctrine.orm.entity_manager"]
    tag:
      - {name: kernel.event_listener, event: oneup_uploader.post_chunk_upload, method: onUpload}

listener类实现了onUpload方法和相应的事件,是不是要监听的rigth事件?

class UploadListener {

   private $doctrine;

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

   public function onUpload(PostChunkUploadEvent $event) {
       //source code
   }
}

前端模板的srcipt

<script>
  YUI().use('uploader', function(Y) {

    var uploader = new Y.Uploader(
            {
                multipleFiles: true,
                uploadURL: "{{ oneup_uploader_endpoint('gallery') }}",
                width: "300px",
                height: "60px"

            }).render("#fileupload");
</script>

我不知道为什么onUpload方法永远不会调用?

3 个答案:

答案 0 :(得分:2)

你必须在你使用语句中使用postPersitentEvent:

use Oneup\UploaderBundle\Event\PostPersistEvent;

答案 1 :(得分:2)

小修正:

//service.yml
amd_picture.uploadListener:
   class: Amd\PictureBundle\Services\UploadListener
   arguments: ["@doctrine.orm.entity_manager"]
      tag:
        - {name: kernel.event_listener, event: oneup_uploader.post_persist, method:     onUpload}

不是标签。是:

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

答案 2 :(得分:1)

YUI3-Uploader无法将文件拆分为多个块,因此YUI3ControllerOneupUploaderBundle也不支持它。这意味着没有post_chunk_upload将被分派,您的EventListener永远不会被调用。

如果您想在成功上传文件后对其进行处理,请尝试按照bundles manual中所述的PostPersistEvent进行处理。

//service.yml
amd_picture.uploadListener:
    class: Amd\PictureBundle\Services\UploadListener
    arguments: ["@doctrine.orm.entity_manager"]
    tag:
        - {name: kernel.event_listener, event: oneup_uploader.post_persist, method:     onUpload}

确保将PostPersistEvent对象传递给侦听器。

use Oneup\UploaderBundle\Event\PostPersistEvent;

class UploadListener {
    public function onUpload(PostPersistEvent $event)
    {
        //...
    }
}