简体   繁体   English

如何使用Sonata Media Bundle上传图像

[英]How to upload an image using Sonata Media Bundle

I am working on a project using symfony.I want to use Sonata Media bundle,in order to upload image. 我正在使用symfony进行项目。我想使用Sonata Media捆绑软件来上传图像。 Unfortunately I don't knwo how to use it or how to start. 不幸的是,我不知道如何使用它或如何开始。

I havethis form : 我有这种形式:

<form action="" method="POST" class="filePhotoForm form-inline" role="form">
                            <div class="form-group">
                                <input type="button" class="btn btn-start-order browse" value="Browse">
                                <input type="text" class="form-control file-name" readonly="readonly" placeholder="No file selected">
                                <input type="file" name="filePhoto" id="filePhoto" class="hidden file-upload">
                            </div><br/><br/>
                            <button type="submit" class="btn btn-primary">Submit</button>
                        </form>

And the code in controller: 和控制器中的代码:

 public function changePictureAction(Request $request)
    {


        return $this->render('MedAppBundle:Profile:change_picture.html.twig');
    }

Can you help with the basics of uploading? 您可以提供有关上传的基础知识吗?

Thank you! 谢谢!

i have made a custom function for that 我为此做了一个自定义功能

protected function saveImageMediaBundle($file,$context = 'default')
{

    $mediaManager = $this->get('sonata.media.manager.media');
    $media = new Media();
    $media->setContext($context);
    $media->setProviderName('sonata.media.provider.image');
    $media->setBinaryContent($file);
    $mediaManager->save($media);

    return $media;
}

and get file by this 并以此获取文件

$file = $request->files->get('newsImage',null);
$media = $this->saveImageMediaBundle($file,'news');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM