I can't figure out how to download a file from the API using Angular. I sending a Blob from the backend but I don't think I convert it the right way since it gives me a serialization error. A bytearray however does give me the content of it but not as download.
This is my Java code:
@GetMapping("{id}")
public byte[] getExambyId(@PathVariable int id) {
Exam exam = new Exam();
byte[] file;
try {
exam = examRepository.findById(id).get();
file = IOUtils.toByteArray(new FileInputStream(storageService.load(exam.getSkelet()).toFile()));
} catch (NoSuchElementException e) {
log.error("Exam with id: " + id + " not found.", this.getClass());
return null;
} catch (StorageFileNotFoundException e) {
log.error("Cannot find examfile: " + exam.getSkelet() + ".", this.getClass());
return null;
} catch (FileNotFoundException e) {
log.error("Cannot find file with filestream: " + exam.getSkelet() + ".", this.getClass());
return null;
} catch (IOException e) {
log.error("IoException on: " + exam.getSkelet() + ".", this.getClass());
return null;
}
return file;
}
This is my service in the frontend:
getExamSkeletById(id) {
window.open(APIURL + '/' + id);
}