yii2:如何使用response->数据返回图像

时间:2017-07-03 06:44:26

标签: yii yii2

public function actionFind()
    {
    $response = Yii::$app->response;
    \Yii::$app->response->format = yii\web\Response::FORMAT_RAW;
    \Yii::$app->response->headers->add('content-type', 'image/jpg');
    $img_url = file_get_contents('images/ab.jpg');
    Yii::$app->response->data($img_url);
    return \Yii::$app->response;
    }

为什么这段代码有错误?

错误是: 无法显示,因为它包含错误

1 个答案:

答案 0 :(得分:1)

数据是属性,而不是函数http://www.yiiframework.com/doc-2.0/yii-web-response.html#$data-detail

public function actionFind()
{
    $response = \Yii::$app->response;
    $response->format = yii\web\Response::FORMAT_RAW;
    $response->headers->add('content-type', 'image/jpg');
    $img_data = file_get_contents('images/ab.jpg');
    $response->data = $img_data;
    return $response;
}