2015-08-26 17:18:50 5702次浏览 2条回答 0 悬赏 10 金钱
Yii::$app->response->content = file_get_contents($file);
Yii::$app->response->headers->set('Content-Type', \yii\helpers\FileHelper::getMimeType($file));
Yii::$app->response->send();
Yii::$app->end();

这个头根本就没有发送出去,还是Content-Type:text/html ; charset=utf-8
到底怎么发啊?

我搞定了,http://stackoverflow.com/questions/27799649/yii2-how-to-response-image-and-let-browser-show-it

我要的效果不是下载,是要下载的话直接Response有自带函数!
是要浏览器直接显示图片:

Yii::$app->response->sendFile($file, null, ['inline' => true]);
Yii::$app->end();

像上面这样就可以了!

  • 回答于 2015-08-26 18:08 举报

    可以试试看用Yii::$app->response->sendFile($file, '下载文件名');代替你的写法

    1 条回复
    回复于 2015-08-26 18:28 回复

    我不是要弹出下载文件框,我是要让浏览器直接显示图片!

  • 回答于 2015-08-27 09:08 举报

    Yii::$app->response->format = Response::FORMAT_RAW;设置编码为图片,下面直接ruturn你的图片就可以了;

    觉得很赞
您需要登录后才可以回答。登录 | 立即注册
dyllen
经理

dyllen

注册时间:2014-09-09
最后登录:2020-10-16
在线时长:48小时47分
  • 粉丝8
  • 金钱400
  • 威望80
  • 积分1680

热门问题