Reputation: 4578
I have this code
return response()->download(storage_path('app/files/gggusers.xlsx'));
in my controller. It executes without any problem but instead of triggering the browser to download the excel file, it just displays a blank white page. I'm positive the file name & location is correct, because if I just change the file name gggusers.xlsx to something else or I delete the file, Laravel will display this error
The file "D:\web\speak\storage\app/files/gggusers.xlsx" does not exist
.
Upvotes: 2
Views: 2024
Reputation: 703
I know this is an old question but I've just had the same problem and solved it. Your problem is that you are returning the response (the file) from download()
to the calling function, index()
, but you are not returning anything from index()
back to the browser tab, hence the blank page.
public function download(){
return response()->download(storage_path('app/files/gggusers.xlsx'));
}
Here you are returning the download file to the index()
function, but in the index()
function you don't return anything back to the browser. So changing this:
public function index(){
$this->download();
}
To this:
public function index(){
return $this->download();
}
will return the file to the browser tab as you'd expect it to.
This is the correct way to do it.
Upvotes: 4
Reputation: 4578
It turns out the issue happens because I put
return response()->download(storage_path('app/files/gggusers.xlsx'));
in another function and call it from the function that's being loaded inside route, something like this:
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
public function index(){
$this->download();
}
public function download(){
return response()->download(storage_path('app/files/gggusers.xlsx'));
}
}
The code above will display a blank page. No errors. The function download is being called without any problem, but somehow after that it just display blank page.
If I just put the code
return response()->download(storage_path('app/files/gggusers.xlsx'));
inside function index()
, the file will be downloaded.
Really appreciate it if someone can explain to me why. Is this some kind of a bug or somehow it's an intended behavior of PHP/Laravel. Wasted a few hours because of this issue.
Upvotes: 1