setDownloadHeaders() public method

Sets a default set of HTTP headers for file downloading purpose.
public setDownloadHeaders ( string $attachmentName, string $mimeType = null, boolean $inline = false, integer $contentLength = null )
$attachmentName string the attachment file name
$mimeType string the MIME type for the response. If null, `Content-Type` header will NOT be set.
$inline boolean whether the browser should open the file within the browser window. Defaults to false, meaning a download dialog will pop up.
$contentLength integer the byte length of the file being downloaded. If null, `Content-Length` header will NOT be set.
 /**
  * Formats the specified response.
  * @param \yii\web\Response $response the response to be formatted.
  */
 public function format($response)
 {
     //$response->getHeaders()->set('Content-Type', 'text/csv; charset=UTF-8');
     $response->setDownloadHeaders(basename(\Yii::$app->request->pathInfo) . '.csv', 'text/csv');
     if ($response->data === null) {
         return;
     }
     $handle = fopen('php://memory', 'r+');
     /** @var array $data should be output of \yii\rest\Serializer configured in current controller */
     $data = $response->data;
     if (!isset($data['items'])) {
         // single model
         fputcsv($handle, array_keys($data));
         fputcsv($handle, array_map(function ($value) {
             return is_array($value) ? print_r($value, true) : (string) $value;
         }, array_values($data)));
     } else {
         // a collection of models
         if (($firstRow = reset($data['items'])) !== false) {
             fputcsv($handle, array_keys($firstRow));
         }
         foreach ($data['items'] as $item) {
             fputcsv($handle, $item);
         }
     }
     rewind($handle);
     // mb_convert_encoding($csv, 'iso-8859-2', 'utf-8')
     $response->content = stream_get_contents($handle);
     fclose($handle);
 }
 /**
  * Formats the specified response.
  * @param \yii\web\Response $response the response to be formatted.
  */
 public function format($response)
 {
     //$response->getHeaders()->set('Content-Type', 'application/vnd.ms-excel');
     $response->setDownloadHeaders(basename(\Yii::$app->request->pathInfo) . '.xls', 'application/vnd.ms-excel');
     if ($response->data === null) {
         return;
     }
     \PHPExcel_Settings::setCacheStorageMethod(\PHPExcel_CachedObjectStorageFactory::cache_to_sqlite3);
     $styles = $this->getStyles();
     $objPHPExcel = new \PHPExcel();
     $sheet = $objPHPExcel->getActiveSheet();
     $offset = 1;
     /*
      * serialize filter
     $sheet->setCellValue('A1', $opcje['nazwaAnaliza']);
     $sheet->duplicateStyle($styles['default'], 'A1:C4');
     $sheet->getRowDimension(1)->setRowHeight(18);
     $sheet->getStyle('A1')->getFont()->setBold(true)->setSize(15);
     $sheet->getStyle('C3:C4')->getFont()->setBold(true);
     $offset = 6;
     */
     $data = $response->data;
     if (!isset($data['items'])) {
         // single model
         $this->addLine($sheet, $offset, array_keys($data));
         $this->addLine($sheet, $offset + 1, array_values($data));
         for ($i = 1, $lastColumn = 'A'; $i < count($data); $i++, $lastColumn++) {
         }
         $sheet->duplicateStyle($styles['header'], 'A' . $offset . ':' . $lastColumn . $offset);
     } else {
         // a collection of models
         if (($firstRow = reset($data['items'])) !== false) {
             $this->addLine($sheet, $offset, array_keys($firstRow));
         }
         $startOffset = ++$offset;
         $item = [];
         foreach ($data['items'] as $item) {
             $this->addLine($sheet, $offset++, $item);
         }
         $this->addSummaryRow($sheet, $startOffset, $offset, $item);
     }
     $filename = tempnam(\Yii::getAlias('@runtime'), 'xls');
     $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
     $objWriter->save($filename);
     $response->content = file_get_contents($filename);
     unlink($filename);
 }