2

I have a link:

    {% for item in list %}
        ...
        <a href="{{ path('show', { 'id': item.id }) }}"> read pdf file</a>
    {% endfor %}

When the user clicks the link I would like to show the pdf file (file stored as a blob in mysql). The code below is not correct, but I want my action to do something like following.

    /**
    * @Route("/show", name="show")
    */
    public function showAction()
    {
        $id = $this->get('request')->query->get('id');
        $item = $this->getDoctrine()->getRepository('MyDocsBundle:Files')->file($id);
        $pdfFile = $item->getFile(); //returns pdf file stored as mysql blob
        $response = new Response();
        $response->setStatusCode(200);
        $response->headers->set('Content-Type', 'application/pdf');
        $response->setContent($pdfFile);
        $response->send() //not sure if this is needed
        return $response;
    }
4

2 に答える 2

2

Doctrine がネイティブに blob タイプを持っているかどうかはわからないので、ファイルを実際の BLOB としてデータベースに適切に保存するようにセットアップされていると仮定します。

これに沿ってもっと何かを試してください...

/**
* @Route("/show/{id}", name="show")
*/
public function showAction($id)
{
    $item = $this->getDoctrine()->getRepository('MyDocsBundle:Files')->find($id);
    if (!$item) {
        throw $this->createNotFoundException("File with ID $id does not exist!");
    }

    $pdfFile = $item->getFile(); //returns pdf file stored as mysql blob
    $response = new Response($pdfFile, 200, array('Content-Type' => 'application/pdf'));
    return $response;
}
于 2012-11-27T12:38:26.577 に答える
1

私は同じ問題に直面していました。これは、エンティティ クラスのフィールドのタイプを変更したためです。「ブロブ」だったので「テキスト」にしました

/**
 * @var string
 *
 * @ORM\Column(name="content", type="text", nullable=false)
 */
private $content;
于 2012-12-19T09:00:29.633 に答える