Tuesday, April 11, 2017

How to download word document from API using Laravel

I am pretty new to using Guzzle with Laravel. I currently just use it for communication between my front-end and a seperate REST api.

I'm trying to download a file from my api but I'm not sure how to go about it. I could just specify the path to the file and directly download it but I also want to be able to stream or view it in my browser so a user can just view a word document instead of just downloading it.

Currently I'm sending a GET request from front end project (with backend to do api calls) to the api project:

$resp = $client->request('GET', env('API_BASE_URL').'/project/'.$id. '/download-report', [ 'headers' => [ 'Authorization' => 'Bearer '. session()->get('api_token') ] ]);

and in my api backend I return the file with the ->download() function.

return response()->download($report->getPath());

Can someone explain what would be the best way to approach this situation?

Solutions for both issues would be awesome, just downloading it, or actually viewing the contents of the word document.

Thanks in advance!



via Joren vh

Advertisement