简体   繁体   中英

Sonata media bundle - get pdf file by name or change filename on update

I currently use SonataAdminBundle and SonataMediaBundle.
I have a problem with the media bundle files because my pdf file is not saved with its name but with md5 or another format.

For example:

  • media table
  • id : 1
  • name : iphone.pdf
  • provider_reference : 9a0b7a35131e23f91666324562c52f08b7cb6604.pdf

I don't want to get my pdf by this:

9a0b7a35131e23f91666324562c52f08b7cb6604.pdf

but with its name ( iphone.pdf ).

I want to know if it's possible to:

  • get the pdf file by name?

or

  • save my pdf file with a particular name?

use this in your Twig so you can download it with its name:

{{ path('sonata_media_download', {'id': (your media object here|sonata_urlsafeid)}) }}

To see it without download, I'm searching for that , I'll be back if I find something.

Good luck!

If you want get public URL for file in PHP-code, you can use something like this (code from my Application\\Sonata\\MediaBundle\\Helper\\MediaHelper)

public function getPublicUrl(Media $media, $format = 'reference')
{
    if (!$media) {
        return '';
    }

    /** @var ImageProvider $provider */
    $provider = $this->container->get($media->getProviderName());
    $format = $provider->getFormatName($media, $format);

    return $provider->generatePublicUrl($media, $format);
}

If you need link in twig, you can find special helper in SonataMedia official docs (I forgot name of helper).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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