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;
}

2 Answers 2

2

I'm not sure Doctrine has a blob type natively, so I'll assume you've got it setup to properly store the file as an actual BLOB in the database.

Try something more along the lines of this...

/**
* @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;
}
Sign up to request clarification or add additional context in comments.

7 Comments

thanks for the response on this. Actually, the pdf files were uploaded and viewable using native php. Here is the error message I get in Symfony: "The Response content must be a string or object implementing __toString(), "resource" given."
My example above assumed the getFile() method for the $item entity would return the raw BLOB data from your table. If you're getting that error then getFile() is returning an object. You'll need to make sure you assign the raw data to the Response object in order for this to work properly. Can you var_dump($pdfFile) to see what is actually in that variable?
var_dump() gives me following: "resource(62) of type (stream)"
Then getFile() is returning an stream object. So you'd have to do something like this $pdfFile = stream_get_contents($item->getFile()) -- This is not the best way to do this, but should work. If that still doesn't work then you really need to figure out what getFile() is actually returning. It's your class isn't it? So what is it?
I created the db from cmd line as follows. php app/console doctrine:generate:entity --entity="MyDocsBundle:Files" (new field name: [file] Field type: [blob]). This created Entity/Files.php that had public function setFile($file) and /**@return string*/public function getFile(). Then I uploaded pdf file (which i can view form a native php test page but not through symfony). Also, the entity Symfony's command tool created for me has the return type as string and not blob, even though I specified file type to be blob).
|
1

I was facing the same problem, for this i Changed the type of field in entity class. it was "blob" i made it "text"

/**
 * @var string
 *
 * @ORM\Column(name="content", type="text", nullable=false)
 */
private $content;

2 Comments

strange, I changing the type to text. It still did not work. I, then truncated my table, uploaded my files again and it worked. I, then changed it back to type=blob. It still worked. It seems my files were not properly uploaded to the db. And, I needed stream_get_contents($file) as lifo suggested below. I am using Symfony2.14. Perhaps, type=blob is supported now. Maybe it wasn't before. Thanks!
yes @AzeemMichael you are right!! in my case i was uploading text file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.