SonataMediaBundle - 如何使用自定义的文件输入自定义、如何使用、文件、SonataMediaBundle

2023-09-14 00:26:03 作者:不疯不颠不青春

我有一种在我使用AngularJS来管理它可能包含不同的问题寻宝项目。这些问题是不同的类型。因此,有些人可能包括文件输入,有些人可能不。在这个项目中,我也使用Symfony和SonataMediaBundle来管理我的文件和我的图片。

I have a kind of scavenger hunt project in which I am using AngularJS to manage the different questions it may contain. These questions are of different types. Therefore, some may include a file input and some may not. In this project, I am also using Symfony and SonataMediaBundle to manage my files and my images.

由于我的HTML模型(主要是我的形式),可以根据用户的行为改变,我无法使用的Symfony的内置工具生成的形式。因此,我所有的表单定制。这给了我SonataMediaBundle一个问题,当我想一些文件被上传。如果用户选择一个文件,该文件将通过POST被发送到控制器中的方法,当形式被发送。因此,我想送这个接收到的文件到SonataMediaBundle,以便它可以管理它,但我还没有在文档中随处可见如何做这样的事情。

Since my html model (mostly my forms) can change depending on the actions of the user, I cannot use Symfony's built-in tool to produce forms. Therefore, all my forms are custom made. This gives me a problem with SonataMediaBundle, when I want some files to be uploaded. If a user selects a file, this file will be sent via POST to a method in the controller, when the form gets sent. Therefore, I want to send this received file to SonataMediaBundle so that it can manage it, but I haven't found anywhere in the documentation how to do such a thing.

从理论上讲,这是非常简单的。在我的控制器,当我得到一个文件输入,我想让SonataMedia管理上传(即拷贝到适当的位置,等...),我有我应该​​怎么做,没有任何线索。

Theoretically, it is really simple. In my controller, when I get a file input, I want to let SonataMedia manage the upload (that is the copy to the proper location, etc...) and I have no clue on how I should do that.

推荐答案

使用Symfony2的,而不是利用它的好处,你正在做一个很大的错误,你应该正确地构建您的应用程序,但据有关方面你的问题没有被symfony的界限,但对自己如何使用it.You可以从容器中的奏鸣曲媒体管理器服务,你必须手动设置所有的媒体管理器所需要的制定者,你必须像文件大小,文件的MIME类型的验证手动工作等等以下是演示如何可以存储在奏鸣曲媒体包中的文件

Using symfony2 and not utilizing its benefits you are doing a big mistake you should built your app properly but as far as concerned to your question nothing is bounded by symfony but its on your own how you use it.You can get the sonata media manager service from the container and you have to manually set the all the required setters for the media manager and you have to manually work for the validations like file size ,file mimetype etc. Below is the demo how you can store the file in the sonata media bundle

/* Before class use these*/
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Application\Sonata\MediaBundle\Entity\Media;

public function uploadAction()
{

$file = $this->get('request')->files->get('filefieldname');

    if (!$file instanceof UploadedFile || !$file->isValid()) {
        return new Response(json_encode(array(
            'event' => 'uploader:error',
            'data'  => array(
                'message' => 'Missing file.',
            ),
        )));
    }

/* validate max min size for the file */
/* validate mime type for the file */
/* Get sonata media manager service from container    */ 
$mediaManager = $this->container->get('sonata.media.manager.media');
/* create new instance of sonata media class in my case entity is located at 
* Application\Sonata\MediaBundle\Entity\Media 
*/
$media = new Media();
$media->setBinaryContent($file);
$media->setContext('default'); 
$ImagemimeTypes = array('image/jpeg', 'image/png');
$FilemimeTypes = array('application/vnd.openxmlformats-officedocument.wordprocessingml.document',
    'application/msword', 'application/pdf', 'application/x-pdf');
if (in_array($file->getMimeType(), $FilemimeTypes)) {
    $media->setProviderName('sonata.media.provider.file');
}
if (in_array($file->getMimeType(), $ImagemimeTypes)) {
    $media->setProviderName('sonata.media.provider.image');
}
 /*If you have other providers in your app like vimeo /dailymotion etc
 then do set here i have shown you the demo for adding image/file in sonata media */
/*other setters if you want to set like enabled/disable etc*/
 $mediaManager->save($media);
// end function
}

但再一次会有很多返工你必须做它的symfony已经为您提供的难易程度。

But once again there will be alot of rework you have to do which symfony already provides you the ease for