- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我尝试了 JQuery 库的新实现:dropzone.js
这个库提供了一个很好的多上传界面和拖放解决方案。
看来Symfony的多上传系统并不简单。
我有这个错误:
Error: Cannot use object of type Symfony\Component\HttpFoundation\File\UploadedFile as array in /vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Csrf/EventListener/CsrfValidationListener.php line 87
我的实体文件:
/**
* @ORM\Entity
* @ORM\Table(name="media__file")
* @ORM\HasLifecycleCallbacks
*/
class File
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank
*/
public $name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $path;
/**
* @Assert\File(maxSize="6000000")
*/
public $file;
public function getAbsolutePath()
{
return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path;
}
public function getWebPath()
{
return null === $this->path ? null : $this->getUploadDir().'/'.$this->path;
}
protected function getUploadRootDir()
{
return __DIR__.'/../../../../../web/'.$this->getUploadDir();
}
protected function getUploadDir()
{
return 'uploads/files';
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->file) {
$this->path = sha1(uniqid(mt_rand(), true)).'.'.$this->file->guessExtension();
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->file) {
return;
}
$this->file->move($this->getUploadRootDir(), $this->path);
unset($this->file);
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if ($file = $this->getAbsolutePath()) {
unlink($file);
}
}
我的 Controller 文件 Controller :
/**
* @Route("/admin/media/upload")
* @Template()
*/
public function uploadsAction (){
$file = new File();
$form = $this->createForm(new \Tperroin\Bundle\MediaBundle\Form\FileUploadType(), $file);
if ($this->getRequest()->isMethod('POST')) {
$form->bind($this->getRequest());
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($file);
$em->flush();
return $this->redirect($this->generateUrl('tperroin_home_default_index'));
}
}
return array('form' => $form->createView());
}
最后是我的 Twig 模板:
<div class="widget-content">
<form action="{{ url('tperroin_media_file_uploads') }}" method="post" {{ form_enctype(form) }} class="dropzone">
<div class="fallback">
<input name="file" type="file" multiple />
</div>
</form>
</div>
编辑:
表单类型:
class FileUploadType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('file', 'file');
}
public function getName()
{
return 'file';
}
}
我的代码有什么问题?我知道这是 UploadedFile 和数组的问题,但我不知道如何解决。
谢谢大家!
编辑:
也许这个链接可以帮助某人,我无法重现这个:
Problems With Multiple File Upload In Symfony2
使用新的 uploadAction 功能进行编辑:
/**
* @Route("/upload/process", name="upload_media")
*/
public function uploadAction()
{
$request = $this->get('request');
$files = $request->files;
$directory = $this->get('kernel')->getRootDir() . '/../web' . $this->getRequest()->getBasePath() . '/files/';
foreach ($files as $uploadedFile) {
$name = $uploadedFile->getClientOriginalName();
$file = $uploadedFile->move($directory, $name);
$upload = new File($name);
$em = $this->get('doctrine')->getManager();
$em->persist($upload);
$em->flush();
}
return $this->redirect($this->generateUrl('tperroin_media_default_index'));
}
最佳答案
首先,您实际上并没有在 Twig 模板中使用 FileUploadType
。您手动创建一个表单并将 action
路径设置为您在 Controller 中定义的 url。您从传递给模板的表单 View 中唯一使用的是 form_enctype
。但与大多数多文件 uploader 一样,Dropzone 似乎伪造了自己的上传图像请求。
这意味着您还必须手动处理传入的数据。
/**
* @Route("/admin/media/upload")
* @Template()
*/
public function showUploadAction()
{
// just show the template
return [];
}
/**
* @Route("/admin/media/upload/process", name="upload_media")
*/
public function uploadAction()
{
$request = $this->get('request');
$files = $request->files;
// configuration values
$directory = //...
// $file will be an instance of Symfony\Component\HttpFoundation\File\UploadedFile
foreach ($files as $uploadedFile) {
// name the resulting file
$name = //...
$file = $uploadedFile->move($directory, $name);
// do something with the actual file
$this->doSomething($file);
}
// return data to the frontend
return new JsonResponse([]);
}
uploadAction
方法从 Symfony 的请求中获取 FileBag
,对其进行迭代,并对生成的文件执行自定义逻辑。当然,您必须自己处理 File
实体的存储。
您的模板应如下所示:
<form action="{{ path('upload_media') }}" method="post" class="dropzone">
<div class="fallback">
<input name="file" type="file" multiple />
</div>
</form>
关于php - 带 Dropzone 的多上传 Symfony 2.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18701684/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!