澄清
这个问题与类似问题的不同之处在于我使用了不同的上传库(Plupload
在这个问题中与FineUploader
在类似问题中)。
在写这个问题的时候,我不知道问题出在哪里——所以我希望使用不同的库可能会找到解决问题的方法。
介绍
三周前OneupUploaderBundle
,我使用FineUploader库创建了关于上传的类似问题。遗憾的是,目前还没有答案。同时我尝试设置不同的上传库(Blueimp)。
一周前,我使用 Blueimp jQuery uplaod 库创建了关于上传的OneupUploaderBundle
类似问题。遗憾的是,目前还没有答案。同时我尝试设置不同的上传库(Plupload)。
我正在Windows 10 Pro
使用XAMPP
[1] 进行开发,其中包括PHP v7.0.8
.
我正在使用 Symfony v3.1.2、OneupUploaderBundle和Plupload库来将文件上传到服务器。
在进行设置时,我遵循了OneUpUploaderBundle
[2] 的文档并Plupload
上传了库 [3]、[4]。
- [2] OneUploaderBundle 文档
- [3] OneUploaderBundle 上传示例
- [4] Plupload上传文档
问题
我想将文件上传到某个目录,然后检查它们的mime 类型并验证是否允许上传的文件 mime 类型,然后 - 将它们移动到自定义目录(可以从文件更改为文件),最后我想保留文件路径和文件名到数据库。
文件上传工作正常,文件上传到oneup_uploader_endpoint('gallery')
. 甚至自定义文件Namer
也可以工作并允许上传到自定义目录。
但是,监听器不会被调用,而是显示在 Symfony Profiler 事件部分Not Called Listeners
!
这很不幸,因为我想将文件移动到自定义目录并将有关文件的信息保存到数据库 unsing Post_Persist
event。此外,验证不称为以太。
代码
我的服务.yml
services:
app.upload_listener:
class: AppBundle\EventListener\UploadListener
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: AppBundle\Uploader\Naming\UploadUniqueNamer
arguments: ["@session"]
我的自定义命名器
<?php
namespace AppBundle\Uploader\Naming;
use Oneup\UploaderBundle\Uploader\File\FileInterface;
use Oneup\UploaderBundle\Uploader\Naming\NamerInterface;
use Symfony\Component\HttpFoundation\Session\Session;
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()
);
}
}
我的 config.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 AppBundle\EventListener;
use Oneup\UploaderBundle\Event\PreUploadEvent;
use Oneup\UploaderBundle\Event\PostUploadEvent;
use Oneup\UploaderBundle\Event\PostPersistEvent;
use Doctrine\ORM\EntityManager;
use AppBundle\Entity\Product;
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 %}
更新
- 还没有
结论
请指教。
感谢您的时间和知识。