怎么用Yii2.0的response發送自己的http頭
怎么用Yii2.0的response發送自己的http頭?
慕蓋茨4494581
2018-11-21 20:11:10
TA貢獻2012條經驗 獲得超12個贊
如果是下載文件,用自帶函數就可以了!
我要的效果是圖片在瀏覽器直接顯示。
像下面這樣寫就可以了
Yii::$app->response->format = \yii\web\Response::FORMAT_RAW; //這句是新加的
Yii::$app->response->content = file_get_contents($file);
Yii::$app->response->headers->set('Content-Type', \yii\helpers\FileHelper::getMimeType($file));
Yii::$app->end();
還有一種方法:
Yii::$app->response->sendFile($file, null, ['inline' => true]);
inline參數為true就是讓瀏覽器顯示,默認false,是下載。
舉報