API to download content as file

Viewed 1267

I would like to create an API which returns a text file. In Symfony I have a controller which is :

            throw $this->createNotFoundException($message);
        }
        $response = new Response();
        $response->headers->set('Content-Type', "text/cfg;charset=utf-8");
        $response->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '.cfg"');
        $response->headers->set('Pragma', "no-cache");
        $response->headers->set('Expires', "0");
        $response->headers->set('Content-Transfer-Encoding', "binary");
        $response->headers->set('Content-Length', strlen($data));
        $response->setContent($data);
        return $response;

Is there any alternative option with api-platform to explain this format ? I'm in symfony 4 with api-plateform

Thanks in advance

1 Answers

You should be able to use symfony's alias I guess (not an api platform user so idk what is installed exactly).

return $this->file($txtPath, 'sample.txt', ResponseHeaderBag::DISPOSITION_ATTACHMENT);
Related