Hello, I am learning to program and I have a problem, when I open a PDF file in my web application to show it, it shows me the following:
Controller Backend (Java)
@GetMapping("/epcanatomia/{rut}/{codigopaciente}")
public ResponseEntity<List<AnatomiaDto>> getAnatomia(@PathVariable("rut") String rut, @PathVariable("codigopaciente") String codigopaciente)
{
try
{
AnatomiaDto a = anatomiapatologicarepository.findAnatomia(rut, codigopaciente).get(0);
File file = new File("C:/APA/" + a.getNombreArchivo());
byte[] bytes = Files.readAllBytes(Paths.get(file.getAbsolutePath()));
AnatomiaDto anatomia = new AnatomiaDto();
anatomia.setNombreArchivo(a.getNombreArchivo());
anatomia.setFile(bytes);
anatomia.setFecha(a.getFecha());
List<AnatomiaDto> list = new ArrayList<AnatomiaDto>();
list.add(anatomia);
return new ResponseEntity<>(list, HttpStatus.OK);
}
catch(Exception e)
{
System.out.println(e.getMessage());
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
- DTO Backend
public class AnatomiaDto {
private String nombreArchivo;
private byte[] file;
private String fecha;
private String rut;
private String codigoPaciente;
public String getNombreArchivo() {
return nombreArchivo;
}
public void setNombreArchivo(String nombreArchivo) {
this.nombreArchivo = nombreArchivo;
}
public byte[] getFile() {
return file;
}
public void setFile(byte[] file) {
this.file = file;
}
public String getFecha() {
return fecha;
}
public void setFecha(String fecha) {
this.fecha = fecha;
}
public String getRut() {
return rut;
}
public void setRut(String rut) {
this.rut = rut;
}
public String getCodigoPaciente() {
return codigoPaciente;
}
public void setCodigoPaciente(String codigoPaciente) {
this.codigoPaciente = codigoPaciente;
}
- Component Frontend (Angular)
Prueba(obj: any){
const blob = new Blob([JSON.stringify(obj.file)], {type: 'application/json'});
const fileURL = URL.createObjectURL(blob);
window.open(fileURL);
}
I have tried changing the following
{type: 'application/json'});
By
{type: 'application/pdf'});
But it sends me an error and tells me that my file cannot be loaded and when reloading it it does not show it to me
So I don't know how to display my PDF file, so when I investigated it told me that it needs to be decoded first in the Backend part but I don't know how to do it and the solutions I saw did not work for me or I did not know how to implement them well I will be attentive to any help, thanks .