canni
canni

Reputation: 5885

How I should construct Response object, to serve images/attachments from DB in Symfony2?

Symfony2 abstracts Request and Response objects, how I should create Response object, to serve client file attachments, and images for browser display?

Upvotes: 2

Views: 5292

Answers (3)

Julien Ducro
Julien Ducro

Reputation: 854

You could simply use the Response object:

use Symfony\Component\HttpFoundation\Response;

And set the Content-Type in it that way:

return new Response($image, 200, array('Content-Type' => 'image/png'));

Upvotes: 10

Cristian Douce
Cristian Douce

Reputation: 3208

Another solution might be this as image is not one of symfony2 supported formats on the Request object.

Also:

static protected function initializeFormats()
{
    static::$formats = array(
        'html' => array('text/html', 'application/xhtml+xml'),
        'txt'  => array('text/plain'),
        'js'   => array('application/javascript', 'application/x-javascript', 'text/javascript'),
        'css'  => array('text/css'),
        'json' => array('application/json', 'application/x-json'),
        'xml'  => array('text/xml', 'application/xml', 'application/x-xml'),
        'rdf'  => array('application/rdf+xml'),
        'atom' => array('application/atom+xml'),
        'rss'  => array('application/rss+xml'),
    );
}

are Symfony's Request object default available formats. Can check in here.

Good luck!

Upvotes: 2

igorw
igorw

Reputation: 28249

You could use the IgorwFileServeBundle which allows you to break the response abstraction to gain performance. If you are using nginx, you can also take advantage of its XSendfile functionaliy.

Note: Lighttpd and Apache have an alternative XSendfile implementation with some differences and are not supported yet. Pull requests welcome.

Upvotes: 2

Related Questions