Home >Backend Development >PHP Tutorial >How to implement file download function in laravel?
There is such a solution in the development documentation.
The principle is to generate a download response and download it automatically when accessing this route, but this way the route is fixed. And the file is fixed. Now I want to implement this function. In a controller, write the file into demo.txt under public. Then download demo.txt to your local computer.
I found that this can only be obtained in response. But cannot download.
. How to implement the download function? Because the downloaded file is generated instantly, the method mentioned in the development document for download to generate a fixed route to respond to the download event is not appropriate.
There is such a solution in the development documentation.
The principle is to generate a download response and download it automatically when accessing this route, but this way the route is fixed. And the file is fixed. Now I want to implement this function. In a controller, write the file into demo.txt under public. Then download demo.txt to your local computer.
I found that this can only be obtained in response. But cannot download.
. How to implement the download function? Because the downloaded file is generated instantly, the method mentioned in the development document for download to generate a fixed route to respond to the download event is not appropriate.
Directly output binary stream.
To output the response header
<code>header('Content-Type: plain/text')</code>