при загрузке файла Excel в Angular файл поврежден. Мы получаем ответ от WEB API в Java. Ниже приведен код.
Любая помощь полезна. Ниже приведен код Java: здесь служба возвращает файл.
public ResponseEntity retrieveByReportType(
@RequestParam("date_from") Date fromDate,
@RequestParam("date_to") Date toDate,
@RequestParam("report_type") ReportType reportType) {
LOG.info("Retrieving payments for reportType : " + reportType);
try {
List<ReportData> reportDataList = serice.retrieveByReportType(fromDate, toDate, reportType);
if (Optional.ofNullable(reportDataList).isPresent()) {
LOG.info("No of Records exists : " + reportDataList.size());
ByteArrayInputStream in = ExcelGeneratorUtil.exportToExcel(reportType, reportDataList);
// return IOUtils.toByteArray(in);
HttpHeaders headers = new HttpHeaders();
String headerValue = "attachment; filename=" + reportType.toString() + ".xls";
headers.add("Content-Disposition", headerValue);
return ResponseEntity
.ok()
.headers(headers)
.contentType(MediaType.parseMediaType("application/vnd.ms-excel"))
.body(new InputStreamResource(in));
} else {
LOG.info("Payment Records not found for Report-Type : " + reportType);
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
} catch (Exception ex) {
throw new PaymentException(ex);
}
}
Служба My Node Js: это место, где явызов WEB API. Здесь я также устанавливаю тип содержимого и тип ответа.
getSelectedReport(req) {
return this.createAuthToken().then(token => request.get({
uri: `${bulkScanUrl}/report/download?date_from=${req.query.date_from}&date_to=${req.query.date_to}&report_type=${req.query.report_type}`,
headers: {
Authorization: `Bearer ${req.authToken}`,
ServiceAuthorization: `Bearer ${token}`,
'Content-type': 'application/json;charset=UTF-8',
'Accept': 'application/vnd.ms-excel'
},
json: true,
responseType: 'arraybuffer'
}));
}
Node Js Controller: This is my Controller where am calling the service controller with the request .
getSelectedReport(req, res) {
return this.bulkScanService.getSelectedReport(req)
.then(result => {
res.status(200).json({ data: result, success: true });
})
.catch(error => {
res.status(500).json({ err: error, success: false });
});
}
Component.ts: This is my component file where am using my blob object to download the excel file. Here am able to download the excel, but the data is corrupt.
this.service.downloadSelectedReport(selectedReportName,selectedStartDate,selectedEndDate).subscribe((response) => {
let myBlob = new Blob([response.data], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
let downloadUrl = URL.createObjectURL(myBlob);
let a = document.createElement('a');
a.href = downloadUrl;
a.download = this.reportsForm.get('selectedreport').value+'.xls';
a.click();
setTimeout( ()=> {
URL.revokeObjectURL(downloadUrl);enter code here
}, 100);`enter code here`
});
}