所以我在这个函数中调用了一个 get 请求:
public exportPendingFile(): void {
this.reset();
this.jhiAlertService.info('bringCockpitApp.LocMgmt.messages.pendingExportStarted');
this.mgmtService.exportPendingFile().subscribe(
(response: HttpResponse<any>) => {
const contentDispositionHeader = response.headers.get('Content-Disposition');
if (contentDispositionHeader) {
const parts: string[] = contentDispositionHeader.split(';');
const filename = parts[1].split('=')[1];
const blob = new Blob([response.body], { type: 'text/plain' });
this.jhiAlertService.success('bringCockpitApp.LocMgmt.messages.pendingExportDone');
saveAs(blob, filename);
} else {
this.jhiAlertService.error('bringCockpitApp.LocMgmt.messages.error');
}
},
(errorResponse: LocResultMessage) => {
this.result = this.formatResultMessage(errorResponse);
this.jhiAlertService.error('bringCockpitApp.LocMgmt.messages.error');
}
);
}
然后我检查这里是否正确发送了标头:
const contentDispositionHeader = response.headers.get('Content-Disposition');
。
由于某种原因,标头没有从后端正确发送。虽然身体被正确发送...
这是TS服务功能:
exportPendingFile(): Observable<any> {
const header = new HttpHeaders({ Accept: 'application/json' });
const options = { headers: header };
return this.http.get(`${this.resourceUrl}/pending/export/en`, options);
}
这是资源:
@GetMapping("/loc-mgmt/pending/export/{baselang}")
@Secured({AuthoritiesConstants.ADMIN, AuthoritiesConstants.ROLE_LOCALISATION_CONTRIBUTOR})
public ResponseEntity exportPendingTranslationFile(@Valid @PathVariable String baselang) {
LOGGER.debug("REST request to export pending translations: {}", baselang);
Optional<Language> potLanguage = languageService.findAll().stream().filter(lang -> lang.getCode().equals(baselang)).findAny();
ExportFileWriter fileWriter = new ICanLocalizeXmlExportFileWriter();
if (potLanguage.isPresent()) {
byte[] bytes = locExportService.exportPendingTranslation(potLanguage.get(), fileWriter);
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, String.format("attachment;filename=pendingExport.%s", fileWriter.getFileFormat().name().toLowerCase()))
.body(bytes);
} else {
return ResponseEntity.badRequest().body(new LocResultMessage("The requested base language does not exist"));
}
}
知道为什么正确返回标头吗?
提前致谢!