I am using react and axios to do a POST to a php service that will return a .xlsx file. The .xlsx file is created properly on the server, but during transit back it gets corrupted, through the Chrome inspector the data seems to be getting converted to a string and many characters are getting changed as a result. Is there a way to do this? Things I have tried:
Headers on request
'Accept': 'application/octet-stream',
'responseType': 'blob',
With the response object
fileDownload(new Blob([response.data]), 'report.xlsx');
Alternatively with the response object
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'file.xlsx');
link.click();
No matter what I try it seems to be getting corrupted. On the server side I have the following PHP code:
$response = new Stream();
$response->setStream(fopen($tempFilePath, 'r'));
$response->setStatusCode(200);
$response->setStreamName($tempFilePath);
$responseHeaders = new Headers();
$responseHeaders->addHeaders(array(
'Content-Disposition' => 'attachment; filename='report.xlsx',
'Content-Type' => 'application/octet-stream',
'Content-Length' => filesize($tempFilePath),
'Expires' => '@0',
'Cache-Control' => 'must-revalidate',
'Pragma' => 'public'
));
$response->setHeaders($responseHeaders);
return $response;