Romeu Stack
Romeu Stack

Reputation: 13

Download file and converter to type doc or pdf

I have a URL that returns a .dat file, I would like to download it but converted to a .pdf or .doc extension, could someone help me?

I'm doing in typescript

URL: http://172.17.0.53:8060/leisarq/\originais\0000000\0000000000353.dat

Upvotes: 0

Views: 2453

Answers (2)

Romeu Stack
Romeu Stack

Reputation: 13

documento.service.ts :

  download(documento: IDocumento) {
    return this.http.post(SERVER_API_URL + "api/download/", documento).subscribe((response) => {
      saveAs(response, documento.tituloDocumento + "." + "pdf");
    });
  }

DocumentResource

    @RequestMapping(value = "/download", method = RequestMethod.POST)
public void download(HttpServletResponse response, @RequestBody DocumentoDTO documentoDTO) throws IOException {
    File file = new File(documentoDTO.getPathorigdoc());
    response.setContentType("pdf");
    response.setHeader("Content-disposition", "attachment;" + documentoDTO.getTituloDocumento());
    response.getOutputStream().write(Files.readAllBytes(file.toPath()));
}

enter image description here

Upvotes: 0

WSD
WSD

Reputation: 3587

Assuming that you just want to grab the file, change the extension on the fly and save it, you can use the file-saver helper (more details here: https://www.npmjs.com/package/file-saver)

I've prepared you a simple example here:

https://stackblitz.com/edit/angular-za1kbf

The idea is quite simple, just get the original file using Angular's http.get() and finally, trigger a download on the client-side using the file-saver utility.

download() {
  return this.http.get("https://i.ibb.co/n3Y038T/a52b055eafb69acd427c99c25bc0fd02.jpg", {
      responseType: "blob"
    }).subscribe((response)=>{
     saveAs(response, "new-image.png");
  });
}

Maybe is worth to mention that this only changes the extension, the file are still a JPG (but with .PNG extension)

Upvotes: 1

Related Questions