gpt4 book ai didi

javascript - 从 Spring Rest api 下载文件

转载 作者:行者123 更新时间:2023-12-02 03:25:42 28 4
gpt4 key购买 nike

我有以下 spring Controller 来下载一个文件,当我直接调用端点时,该文件工作正常,并获得一个包含加密内容的 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);
}

我的 javascript 上有以下代码来调用端点并下载文件。问题是我可以下载该文件,但无法解密它,而当我通过直接调用端点下载文件时,它会被解密。

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(''));

调用服务器:

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);
} }

我在这里做错了什么?我查看了数十个示例,这就是文件下载的方式。

谢谢!

最佳答案

我最终添加了如下所示的 DTO 对象,并更改了我的其余 Controller ,如下所示:

@Data
public class RegisteredCardsReport {

public RegisteredCardsReport(byte[] encryptedReport, String fileName) {
this.encryptedReport = encryptedReport;
this.fileName = fileName;
}

byte[] encryptedReport;
String fileName;
}

//Rest端点改变

@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);
}

最后使用本文中接受的答案来创建文件: Download File from Bytes in JavaScript

关于javascript - 从 Spring Rest api 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56907893/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com