我正在尝试在 angular 项目的 Assets 中下载从 spring boot 生成的文件。当我从 angular 服务调用 spring API 时,angular CLI 在 assets angular 文件夹中创建文件后重新编译项目,然后在从 spring boot API 获得响应之前重新加载页面。
我尝试通过多种方式从 angular 调用 spring boot API:
我不知道如何继续
Spring Boot
@GetMapping(path = "/downloads/{fileId}", produces = "application/json")
@ResponseBody
public ResponseEntity<String> download(@PathVariable String fileId){
Gson gson = createGson();
List<com.bioimis.blp.entity.File> fileById;
try {
fileById = fileRepository.findFileById(fileId);
} catch (Exception e) {
logger.error(e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
if (fileById.isEmpty()) {
logger.error(fileId + " not found");
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
}
if(fileById.get(0).getDeletionDate() != null) {
List<String> phrases = new ArrayList<>();
try {
phrases = translationRepository.getAllTranslationsByFileId(fileById.get(0).getId());
} catch (Exception e) {
logger.error(e.getMessage());
return ResponseEntity.status(HttpStatus.OK).body(null);
}
String file = "";
for (int i = 0; i < phrases.size(); i++) {
file = file.concat(phrases.get(i));
}
file = file.concat("\0");
/*Suppongo che prima dell'estensione gli ultimi 5 caratteri del file sono in formato 'languageCode_countryCode'*/
String nameFile = fileById.get(0).getPath().split("/")[fileById.get(0).getPath().split("/").length - 1];
Language language;
try {
language = languageRepository.findLanguageById(fileById.get(0).getLanguageId()).get(0);
} catch (Exception e) {
logger.error(e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
int i = StringUtils.lastIndexOf(nameFile, '.');
String ext = StringUtils.substringAfter(nameFile, ".");
nameFile = nameFile.substring(0, i - 5) + language.getLanguageCode() + "_" + language.getCountryCode() + "." + ext;
Path path = Paths.get(pathDownload + "/" + nameFile);
try {
-----------------------------------------------------------------------------
Files.write(path, file.getBytes());
------------>after this instruction, angular reloads the page<---------------
-----------------------------------------------------------------------------
} catch (IOException e) {
logger.error(e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
return ResponseEntity.status(HttpStatus.OK).body(gson.toJson(path.toString()));
}
return ResponseEntity.status(HttpStatus.OK).body(null);
}
Angular 组件
downloadFile(fileId: string) {
let link: string;
this.http.downloadFile(fileId).subscribe(
data => {
link = data;
}
);
console.log(link);
return link;
}
Angular 服务
downloadFile(fileId: string) {
const myheader = new HttpHeaders().set('Authorization', this.token);
return this.http.get<string>(this.url.concat('files/downloads/' + fileId), { headers: myheader });
}
我只是希望在不重新加载 Angular 组件的情况下从 spring boot api 获得响应。
(在 postman 中,它必须按原样工作)
最佳答案
启动 Angular 应用程序(而不是 ng serve
)
ng serve --live-reload false
--liveReload=true|false
Whether to reload the page on change, using live-reload.Default: true
Angular Commands
备择方案
ng serve --no-live-reload
ng serve --live-reload=false
"ng noreload" : "ng serve --live-reload=false"
关于html - Angular:当 Assets 文件夹发生变化时防止自动重新编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57919343/