>澄清
这个问题与类似问题的不同之处在于我使用的是不同的上传库(Plupload
这个问题中的FineUploader
与类似问题中的(。
在撰写此问题时,我不知道问题出在哪里 - 因此我希望使用不同的库可能会显示解决问题的方法。
介绍
三周前,我创建了关于使用 OneupUploaderBundle
上传的类似问题,但使用 FineUploader 库。可悲的是,目前还没有答案。与此同时,我尝试设置不同的上传库(Blueimp(。
一周前,我创建了关于使用 OneupUploaderBundle
上传的类似问题,但使用 Blueimp jQuery uplaod 库。可悲的是,目前还没有答案。与此同时,我尝试设置不同的上传库(Plupload(。
我正在Windows 10 Pro
开发XAMPP
[1],其中包括PHP v7.0.8
.
- [1] XAMPP for Windows
我正在使用Symfony v3.1.2,OneupUploaderBundle和Plupload库来上传文件到服务器。
在设置时,我遵循了OneUpUploaderBundle
[2] 的文档和Plupload
上传库 [3]、[4]。
- [2] OneupUploaderBundle 文档
- [3] OneupUploaderBundle Plupload 示例
- [4] 上传上传文档
问题
我想将文件上传到某个目录,然后检查它们的 mime 类型并验证是否允许上传文件 mime 类型,然后 - 将它们移动到自定义目录(可以从文件更改为文件(,最后我想将文件路径和文件名保存到数据库。
文件上传工作正常,文件上传到oneup_uploader_endpoint('gallery')
。即使是自定义文件Namer
也可以工作,并允许上传到自定义目录。
但是,侦听器不会被调用,并显示在Symfony Profiler Events部分Not Called Listeners
!
这很不幸,因为我想将文件移动到自定义目录并将有关文件的信息保存到数据库取消Post_Persist
事件。此外,验证不称为以太币。
法典
我的服务.yml
services:
app.upload_listener:
class: AppBundleEventListenerUploadListener
arguments: ["@doctrine.orm.entity_manager"]
tags:
- { name: kernel.event_listener, event: oneup_uploader.post_persist.default_uploader, method: onPostUpload }
- { name: kernel.event_listener, event: oneup_uploader.post_persist.default_uploader, method: onUpload }
app.upload_unique_namer:
class: AppBundleUploaderNamingUploadUniqueNamer
arguments: ["@session"]
我的自定义命名器
<?php
namespace AppBundleUploaderNaming;
use OneupUploaderBundleUploaderFileFileInterface;
use OneupUploaderBundleUploaderNamingNamerInterface;
use SymfonyComponentHttpFoundationSessionSession;
class UploadUniqueNamer implements NamerInterface
{
private $session;
public function __construct(Session $session)
{
$this->session = $session;
}
/**
* Creates a user directory name for the file being uploaded.
*
* @param FileInterface $file
* @return string The directory name.
*/
public function name(FileInterface $file)
{
$upload_files_path = $this->session->get('upload_files_path');
$unique_name = uniqid();
return sprintf('%s/%s_%s',
$upload_files_path,
$unique_name,
$file->getClientOriginalName()
);
}
}
我的配置.yml
oneup_uploader:
mappings:
gallery:
frontend: plupload
enable_progress: true
namer: app.upload_unique_namer
use_orphanage: false
allowed_mimetypes: [image/png, image/jpg, image/jpeg, image/gif]
max_size: 200M
我的上传监听器:
<?php
namespace AppBundleEventListener;
use OneupUploaderBundleEventPreUploadEvent;
use OneupUploaderBundleEventPostUploadEvent;
use OneupUploaderBundleEventPostPersistEvent;
use DoctrineORMEntityManager;
use AppBundleEntityProduct;
class UploadListener
{
/**
* @var EntityManager
*/
private $entityManager;
//protected $originalName;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function onUpload(PostPersistEvent $event)
{
$file = $event->getFile();
$this->originalName = $file->getClientOriginalName();
var_dump('In -- OnUpload');
die();
}
public function onPostUpload(PostPersistEvent $event)
{
$file = $event->getFile();
$object = new Product();
$object->setName($file->getClientOriginalName());
//$object->setName($file->getPathName());
$this->entityManager->persist($object);
$this->entityManager->flush();
var_dump('In -- OnPostUpload');
die();
}
}
上传.html.twig
{% extends 'base.html.twig' %}
{% block stylesheets %}
{{ parent() }}
<link type="text/css" rel="stylesheet" href="{{ asset('js/jquery-ui-1.12.0/jquery-ui.css') }}" />
<link type="text/css" rel="stylesheet" href="{{ asset('js/plupload/jquery.ui.plupload/css/jquery.ui.plupload.css') }}" media="screen" />
{% endblock %}
{% block content %}
<div id="box-upload">
<div id="uploader">
<p>Your browser doesn't have HTML5 support.</p>
</div>
</div>
{% endblock %}
{% block javascripts %}
<script type="text/javascript" src="{{ asset('js/jquery-2.2.4.js') }}"></script>
<script type="text/javascript" src="http://bp.yahooapis.com/2.4.21/browserplus-min.js"></script>
<script type="text/javascript" src="{{ asset('js/jquery-ui-1.12.0/jquery-ui.js') }}"></script>
<script type="text/javascript" src="{{ asset('js/plupload/plupload.full.min.js') }}"></script>
<script type="text/javascript" src="{{ asset('js/plupload/jquery.ui.plupload/jquery.ui.plupload.js') }}"></script>
<script type="text/javascript">
$(function()
{
$("#uploader").plupload(
{
// General settings
runtimes : 'html5',
url: "{{ oneup_uploader_endpoint('gallery') }}",
multi_selection: false,
// Maximum file size
max_file_size: '10mb',
chunk_size: '1mb',
// Resize images on clientside if we can
resize: {
width: 200,
height: 200,
quality: 90,
crop: true // crop to exact dimensions
},
// Specify what files to browse for
filters: [
{title: "Image files", extensions : "jpg,jpeg,png,gif"},
{title: "Zip files", extensions : "zip,avi"}
],
// Rename files by clicking on their titles
rename: true,
// Sort files
sortable: true,
// Enable ability to drag'n'drop files onto the widget (currently only HTML5 supports that)
dragdrop: true,
// Views to activate
views: {
list: true,
thumbs: false, // Show thumbs
active: 'list'
},
// Flash settings
flash_swf_url: '/plupload/js/Moxie.swf',
// Silverlight settings
silverlight_xap_url: '/plupload/js/Moxie.xap'
});
});
</script>
{% endblock %}
更新
- 还没有
结论
请指教。
感谢您的时间和知识。
有问题的问题是由services.yml
中event listeners
配置错误引起的。这是工作配置。
我的服务.yml
services:
app.upload_listener:
class: AppBundleEventListenerUploadListener
arguments: ["@doctrine.orm.entity_manager"]
tags:
- { name: kernel.event_listener, event: oneup_uploader.post_persist.gallery, method: onPostUpload }
- { name: kernel.event_listener, event: oneup_uploader.post_persist.gallery, method: onUpload }
app.upload_unique_namer:
class: AppBundleUploaderNamingUploadUniqueNamer
arguments: ["@session"]