Я пытаюсь экспортировать данные Grid в React JS в Excel и PDF.Я использую React JS в качестве внешнего интерфейса и Spring RestAPI в качестве службы.Ниже приведен код для Сервиса.Когда я вызываю эти службы из React, я получаю байтовые коды в объекте ответа, и экспорт или pdf не генерируются.
@PostMapping(value = "/arcadesExportToPDF", produces = MediaType.APPLICATION_PDF_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<InputStreamResource> arcadesExportToPDF(
@RequestBody arcadesSearch arcadesSearch ) throws IOException {
List<ArcadesSearchResult> arcadesSearchList = arcadesSearchService.searchArcades(arcadesSearch);;
ByteArrayInputStream in = GeneratePDFReport.arcadesToPDF(arcadesSearchList);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment; filename=arcadesSearch.pdf");
return ResponseEntity.ok().headers(headers).contentType(MediaType.APPLICATION_PDF).body(new InputStreamResource(in));
}
@PostMapping(value = "/arcadesExportToExcel", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<InputStreamResource> arcadesExportToExcel(
@RequestBody arcadesSearch arcadesSearch ) throws IOException {
List<ArcadesSearchResult> arcadesSearchList = arcadesSearchService.searchArcades(arcadesSearch);;
ByteArrayInputStream in = GenerateExcelReport.arcadesToExcel(arcadesSearchList);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment; filename=arcadesSearch.xlsx");
return ResponseEntity.ok().headers(headers).contentType(MediaType.APPLICATION_OCTET_STREAM).body(new InputStreamResource(in));
}
Теперь вот код от React JS.Мы используем axios для вызова веб-службы Rest.
export function makePostCall(url, postData, state, contentType, retryCount=0) {
const postUrl= url;
const headerObj = contentType ? {
"Accept": "application/json"
} : {
"Content-Type": "application/json; charset=utf-8",
"Accept": "application/json; application/pdf"
};
const options = {
method: "POST",
headers: new Headers(headerObj),
credentials: "same-origin",
data: contentType ? postData : JSON.stringify(postData)
};
return axios(postUrl, options).then(function(response) {
return processResponse(response);
}).catch(error => {
if(error.name.toUpperCase()==="TYPEERROR" && error.message.toUpperCase() === "NETWORK REQUEST FAILED" && retryCount<maxRetryCount){
retryCount++;
return makePostCall(url, postData, state, contentType, retryCount);
}
return Promise.reject(genericError);
});
}
Любая помощь очень важна.Заранее спасибо