# Download file empty

**URL:** <https://discourse.cakephp.org/t/download-file-empty/9290>\
**Category:** Need Help\
**Created:** [April 24, 2021, 10:37am UTC](https://discourse.cakephp.org/t/download-file-empty/9290 "2021-04-24T10:37:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kani](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kani/32/515_2.png) [@kani](https://discourse.cakephp.org/u/kani)\
**Post date:** [April 24, 2021, 10:37am UTC](https://discourse.cakephp.org/t/download-file-empty/9290/1 "2021-04-24T10:37:12Z")

</div>

CakePHP 4  
php 8  
Query result coming but downloaded file empty.

```
public function export($format = 'json')
    {

        $format = strtolower($format);
        $formats = [
            'xml' => 'Xml',
            'json' => 'Json',
        ];

        if (!isset($formats[$format])) {
            throw new NotFoundException(__('Unknown format.'));
        }

        $this->viewBuilder()->setClassName($formats[$format]);
        $visitors = $this->Visitors->find();
        // dd($visitors->toArray());
        $this->set(compact('visitors'));
        $this->viewBuilder()->setOption('serialize', ['visitors']);
        return $this->response->withDownload('report-' . date('YmdHis') . '.' . $format);
    }
```

---

<div class="post-metadata">

**Author:** ![raul338](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/raul338/32/69_2.png) [@raul338](https://discourse.cakephp.org/u/raul338)\
**Post date:** [April 25, 2021, 3:33am UTC](https://discourse.cakephp.org/t/download-file-empty/9290/2 "2021-04-25T03:33:20Z")

</div>

replace the return with

```php
$this->response = $this->response->withDownload('report-' . date('YmdHis') . '.' . $format);

```

The view is rendered if your controller action doesn’t return or return null
