I want to download the zip file from the server using PHP Laravel. The Laravel passes zip files through following return command:
return response()->download($file);
It downloads the zip file when I hit the route URL for the controller in the browser, and it also downloads the zip file when I use window.open(url)
in the Angular but it is not secure as anyone can download the file. The user has to authenticate before downloading the file but I cannot pass header with window.open(url)
, therefor I need a different approach to achieve this.
I went through most of the questions here, and they suggest to use FileSaver and Blob. I tried to test with those but didn't work and it's because the API doesn't pass the zip file to the client.
Here is the Angular http.get:
return http.get('localhost:8000/dashboard/backup')
.subscribe(res => {
console.log('Successfull');
}, err => {
console.log(err);
});
I can get the file path, but the path is not public and cannot download it using Angular. The path is returned as Application/XAMP.... and when I set it as HREF and click on it, it loads in browser as localhost:4200/Application/XAMP....
In the console log, I have an invalid character at JSON file line 1. Why is the zip file passed as JSON?