1

i want download my pdf file where he's saved in my phpmyadmin 'blob'

i try this code:

public function downloadAction()
{
    $ma_base_de_donne = new myprojectdbEntity();

    $item = $this->getDoctrine()->getRepository('myprojectBundle:myprojectdbEntity')->find(5);
    if (!$item) {
        throw $this->createNotFoundException("File with ID 5 does not exist!");
    }
    $pdfFile = $item->getFichier(); //returns pdf file stored as mysql blob
    //$headers = array('Content-Type' => 'application/pdf', 'Content-Disposition' => "attachment; filename=" . urlencode($pdfFile));
    $response = new Response($pdfFile, 200, array('Content-Type' => 'application/pdf'));
    //return $response;
    var_dump($response); die();
}

and i get this error :

The Response content must be a string or object implementing __toString(), "resource" given.

help me please

thanks first

  • Try toString($pdfFile) as the first parameter of the response object? – Tony Mar 29 '17 at 21:56
  • looks like you are getting a resource. Try a `\Doctrine\Common\Util\Debug::dump($item);exit;` and see what is in it. – Ibu Mar 29 '17 at 22:03
  • 1
    Possible duplicate of [Symfony2: How to display/download a BLOB field](http://stackoverflow.com/questions/15206963/symfony2-how-to-display-download-a-blob-field) – Alister Bulman Mar 29 '17 at 22:04

1 Answers1

0

Define a __toString() method in your entity object($pdfFile):

public function __toString() {
    return $name;
}

Or send directly the string in your response:

$response = new Response($pdfFile->getName(), 200, array('Content-Type' => 'application/pdf'));
Cristian Bujoreanu
  • 1,147
  • 10
  • 21