【问题标题】:yii2 : how to return image with response->datayii2 : 如何使用 response->data 返回图像
【发布时间】:2017-07-03 17:08:47
【问题描述】:
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;
    }

为什么这段代码有错误??

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

【问题讨论】:

  • 请显示错误..你的代码..还不够..添加动作/功能代码..
  • 无法显示,因为它包含错误
  • 我测试过:$img_url = file_get_contents('images/ab.jpg');我又遇到了错误
  • 我想要创建链接,例如这个链接:manage.pyra.ir/…
  • 如果您需要链接为什么要加载图像..您应该只指定路径

标签: yii yii2


【解决方案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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-15
    • 1970-01-01
    • 2021-07-30
    • 2021-10-07
    • 2017-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多