Hi Yii2 people,
I’m trying to send a big file (several Mo) to the browser and to avoid reaching any PHP memory limit I want to send it as a stream.
This is what I wrote in the controller action :
Yii::$app->response->format = Response::FORMAT_RAW;
Yii::$app->response->headers->set('Content-type',$doc->mime_type);
Yii::$app->response->headers->set('Content-Disposition','filename="' . $doc->original_filename . '"');
Yii::$app->response->headers->set('Content-Transfer-Encoding','binary');
Yii::$app->response->headers->set('Content-Length',$doc->file_size);
Yii::$app->response->headers->set('Accept-Ranges','bytes');
$stream = fopen('F:/Temp/documents/bf902ce3f994b3e182d18a5f261ba737.pdf', 'rb');
Yii::$app->response->sendStreamAsFile($stream);
Yii::$app->response->send();
And this the what I get :
exception 'yii\base\InvalidParamException' with message 'Response content must not be an array.' in F:\Project\ws1\yii2-my-archive-master\app\vendor\yiisoft\yii2\web\Response.php:935
Stack trace:
#0 F:\Project\ws1\yii2-my-archive-master\app\vendor\yiisoft\yii2\web\Response.php(311): yii\web\Response->prepare()
#1 F:\Project\ws1\yii2-my-archive-master\app\vendor\yiisoft\yii2\web\ErrorHandler.php(110): yii\web\Response->send()
#2 F:\Project\ws1\yii2-my-archive-master\app\vendor\yiisoft\yii2\base\ErrorHandler.php(95): yii\web\ErrorHandler->renderException(Object(yii\base\ErrorException))
#3 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\base\ErrorException))
#4 {main}
HTTP headers are correct, but I’m not sure about how to use the sendAsStream method to achieve what I want. Is it the appropriate way ?
Any help is appreciated.
ciao