symfony2 download file Response内容必须是字符串

时间:2017-03-29 21:54:37

标签: php symfony-2.5

我想下载我的pdf文件,并将其保存在我的phpmyadmin'blob'

我试试这段代码:

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

我收到此错误:

  

Response内容必须是字符串或对象实现   __toString(),“资源”给出。

请帮助我

首先感谢

1 个答案:

答案 0 :(得分:0)

在实体对象中定义__toString()方法($ pdfFile):

public function __toString() {
    return $name;
}

或直接发送回复中的字符串:

$response = new Response($pdfFile->getName(), 200, array('Content-Type' => 'application/pdf'));