File download on base 64 (Controller)

0

I have the following situation: I have a View that calls a link to Controller to download the file (without leaving the page). I was able to make it work by reading the file from the server, but what I am trying to do is to return the file by Controller without having the file on the server:

View:

<?= $this->Html->link('Download', ['controller' => 'Comunicados', 'action' => 'testeDownload']) ?>

Controller:

public function testeDownload(){
        $arquivo = chunk_split(base64_encode(file_get_contents(WWW_ROOT . 'uploads/0ba71f3d77730cb8671b8faafe8efceb.pdf')));
        $this->response->file($arquivo, 
            array(
                'download' => true
            )
        );
        return $this->response;
    }

I'm trying to do this, without passing the file path to the file() function because my intention BEFORE returning the file is to delete it from the server.

How I made it work just by passing the URL of the file:

public function testeDownload(){
        $caminho = WWW_ROOT . 'uploads/0ba71f3d77730cb8671b8faafe8efceb.pdf';
        $this->response->file($caminho, 
            array(
                'download' => true
            )
        );
        return $this->response;
    }

Edit:

I did not find any information in the CakePHP documentation or questions here in Stack Overflow. Nothing regarding VIA Base64 file download. Download file via path I found several examples.

    
asked by anonymous 02.02.2018 / 21:04

0 answers