After receiving a file from a server using AJAX (Angular), the file, a simple XLSX document
, is sent as shown below:
ob_start();
$file = \PHPExcel_IOFactory::createWriter($xls, 'Excel2007');
$file->save('php://output');
$response->setContent(ob_get_clean());
$response->headers->replace(array(
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'Content-Disposition' => 'attachment;filename=file.xlsx"'
));
When making a request from the frontend, the Accept header is used. The file is saved using angular-file-saver with FileSaver.js and Blob.js. However, the received file is corrupt and cannot be opened in Excel; its size is (for example) 12446 bytes, while Chrome's DevTools Network tab displays a Content-Length header of 7141 bytes.
How can this issue be resolved?
UPD: The request is sent in the following manner:
$http.get(baseURL + '/' + entity + '/export/?' + condition + sort, {
headers: {'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8'}
});
The file is downloaded as follows:
var data = new Blob([response.data], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'});
FileSaver.saveAs(data, 'file.xlsx');