[英]Downloading a file from spring rest api
I have following spring controller to download a file which is working fine when I directly call the endpoint and I get a csv file with encrypted content . 我有以下spring控制器来下载一个文件,当我直接调用端点并获得带有加密内容的csv文件时,该文件运行良好。
@GetMapping(value = "registered-cards")
public ResponseEntity<byte[]> generateRegisteredCards(@RequestParam("from") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) ZonedDateTime from,
@RequestParam("to") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) ZonedDateTime to) throws Exception {
byte[] bytes = cfsbReportingService.generateRegisteredCardsReport(from, to);
HttpHeaders headers = new HttpHeaders();
headers.add("Access-Control-Allow-Origin", "*");
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + "report-" + LocalDateTime.now() + ".csv");
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
}
I have following code on my javascript to call the endpoint and download the file. 我的JavaScript上有以下代码来调用端点并下载文件。 The thing is I can download the file but I cannot decrypt it whereas when I download the file by directly calling the endpoint, it gets decrypted. 问题是我可以下载文件,但不能解密,而直接调用端点下载文件时,文件被解密了。
public getRegisteredCards(fromDate, toDate) : void {
const fromEst = fromDate.startOf('day').tz('America/New_York').format();
const endEst = toDate.endOf('day').tz('America/New_York').format();
this.reportingService.generateNewRegisteredCardsFile(fromEst, endEst).then(
(response:any) => {
const blob = new Blob([response.data], {type: 'application/octet-stream'});
const hiddenElement = document.createElement('a');
hiddenElement.href = window.URL.createObjectURL(blob);
hiddenElement.target = '_blank';
hiddenElement.download = 'file.csv';
hiddenElement.click();
}
).catch(this.handleError(''));
Call to server: 呼叫伺服器:
public generateNewRegisteredCardsFile(from: String, to: String) {
const url = `${this.api()}/reporting/v1/registered-cards?from=${from}&to=${to}` ;
const headers = new Headers({'Content-Type': 'application/octet-stream', 'Accept': 'application/octet-stream', 'Access-Control-Allow-Origin': '*'});
return this.$http.get(url, headers);
} }
What am I doing wrong here? 我在这里做错了什么? I looked at tens of examples and that's how file gets downloaded. 我看了几十个示例,这就是文件的下载方式。
Thank you! 谢谢!
I ended up adding a DTO object like below and changed my rest controller as follows: 我最终添加了如下所示的DTO对象,并如下更改了我的rest控制器:
@Data
public class RegisteredCardsReport {
public RegisteredCardsReport(byte[] encryptedReport, String fileName) {
this.encryptedReport = encryptedReport;
this.fileName = fileName;
}
byte[] encryptedReport;
String fileName;
}
//Rest Endpoint change //剩余端点更改
@GetMapping(value = "new-registered-cards")
public ResponseEntity<RegisteredCardsReport> generateNewRegisteredCards(@RequestParam("from") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) ZonedDateTime from,
@RequestParam("to") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) ZonedDateTime to) {
byte[] encryptedRpt = ReportingService.generateRegisteredCardsReport(from, to);
HttpHeaders headers = new HttpHeaders();
headers.add("Access-Control-Allow-Origin", "*");
RegisteredCardsReport cardsReport = new RegisteredCardsReport(encryptedRpt, "registered-cards--" + LocalDateTime.now() + ".csv");
return new ResponseEntity<>(cardsReport, headers, HttpStatus.OK);
}
and finally used the accepted answer in this post to create file: Download File from Bytes in JavaScript 最后使用本文中接受的答案来创建文件: 从JavaScript中的字节下载文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.