我這樣做。我添加了另一個功能只是爲了設置標題。您可以在幫助推動這一功能太:
$this->setHttpHeaders('csv', 'filename', 'text/plain');
/**
* Sets the HTTP headers needed by file download action.
*/
protected function setHttpHeaders($type, $name, $mime, $encoding = 'utf-8')
{
Yii::$app->response->format = Response::FORMAT_RAW;
if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE") == false) {
header("Cache-Control: no-cache");
header("Pragma: no-cache");
} else {
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: public");
}
header("Expires: Sat, 26 Jul 1979 05:00:00 GMT");
header("Content-Encoding: {$encoding}");
header("Content-Type: {$mime}; charset={$encoding}");
header("Content-Disposition: attachment; filename={$name}.{$type}");
header("Cache-Control: max-age=0");
}
我也發現瞭如何yii2它,看看這裏(滾動至底部)https://github.com/yiisoft/yii2/blob/48ec791e4aca792435ef1fdce80ee7f6ef365c5c/framework/captcha/CaptchaAction.php
請解釋一下你的問題 – soju