【问题标题】:symfony2 download file The Response content must be a stringsymfony2 下载文件 Response 内容必须是字符串
【发布时间】:2017-03-31 15:21:12
【问题描述】:

我想下载我保存在我的 phpmyadmin 'blob' 中的 pdf 文件

我试试这个代码:

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

我得到这个错误:

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

请帮帮我

先谢谢

【问题讨论】:

  • 试试toString($pdfFile)作为响应对象的第一个参数?
  • 看起来您正在获取资源。试试\Doctrine\Common\Util\Debug::dump($item);exit; 看看里面有什么。

标签: php symfony-2.5


【解决方案1】:

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

public function __toString() {
    return $name;
}

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

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

【讨论】:

    猜你喜欢
    • 2018-02-05
    • 1970-01-01
    • 2022-08-03
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 2019-10-06
    • 2018-01-03
    相关资源
    最近更新 更多