我具有以下触发数据导出并下载响应的Angular组件:
exportData(): void {
this.exporting = true;
this.changeButton();
const options = {headers: new HttpHeaders({'Content-Type': 'application/json'}), responseType: 'arraybuffer' as 'json'};
this.http.post('http://localhost:8080/export/lb', JSON.stringify(this.filter), options)
.subscribe((next) => {
// @ts-ignore
const file = new Blob([next], {type: 'application/pdf'});
const fileURL = URL.createObjectURL(file);
window.open(fileURL);
this.exporting = false;
this.changeButton();
}, error1 => {
this.exporting = false;
this.changeButton();
});
}
过滤器现在将包含一个新的导出格式选项CSV。
这是我的后端代码:
@CrossOrigin
@RequestMapping(value = "/export/lb", method = RequestMethod.POST)
public ResponseEntity<byte[]> getFilterLBPDF(@RequestBody PdfExportFilter filter) throws IOException, DocumentException {
F5StructureExporter writer = ExporterFactory.getInstance.getExporter(filter);
Path filePath = Paths.get(writer.getBaseTempDir() + "/filteredLBexport.pdf");
writer.exportData();
F5StructureExporter writerCsv = new FilteredCsvLbExporter(filter);
writerCsv.exportData();
return new ResponseEntity<byte[]>(readPdfBytes(filePath), getHttpHeaders("filtered_export"), HttpStatus.OK);
}
private HttpHeaders getHttpHeaders(String file) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/pdf"));
String filename = new Date() + "_" + file;
headers.setContentDispositionFormData(filename, filename);
headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
return headers;
}
private byte[] readPdfBytes(Path path) {
try {
return Files.readAllBytes(path);
} catch (IOException e) {
e.printStackTrace();
}
return new byte[0];
}
现在,我希望也能返回CSV,是否有机会在Angular中确定PDF或CSV?
非常感谢。