Download a file generated in the backend by Angular2+

Asked

Viewed 1,045 times

0

Hello, I’m having trouble downloading a file generated by backend in my angular project, I have the following method

generateXlsx(obj) {
let headers = new Headers();
let token = localStorage.getItem('token');
let client = localStorage.getItem('client');
let uid = localStorage.getItem('uid');
headers.append('access-token', token);
headers.append('client', client);
headers.append('uid', uid);
return this.http.get(this.xlsxUrl, { params: obj, headers: headers });
}

When it is called it goes to Download url of my file, but when I call this Route/Url via angular, it does not download, but in the Network tab the file is saved in Answer, follow print of the result Codigo gerado no response

In my Frontend, when I press a button and call the following method

generatePlanilha(obj) {
this.api.generateXlsx(obj)
  .subscribe(response => {
    if (response) {
      console.log("Planilha Gerada");
    }
  },
    (error => { console.log("NAO GEROU PLANILJA") }));
  }

I was wondering how I do to download the answer that is on the Network

1 answer

1

You can create a blob from Response and download it by opening a popup with the data:

 import 'rxjs/add/operator/map';

 .map((res:Response) => res.text())
 .subscribe(response => {
    if (response) {
      const blob = new Blob([response], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
      const url= window.URL.createObjectURL(blob);
      window.open(url);
    }
  },
    (error => { console.log("NAO GEROU PLANILJA") }));
  }

https://developer.mozilla.org/en-US/docs/Web/API/Blob

https://developer.mozilla.org/en-US/docs/Web/API/Window/open

https://stackoverflow.com/questions/2937465/what-is-correct-content-type-for-excel-files

  • This gives the following error in "[Response]" [ts] Type 'Response' is not Assignable to type 'Blobpart'. Type 'Response' is not Assignable to type 'Blob'. Property 'size' is Missing in type 'Response'. [2322] (Parameter) Answer: Response

  • Try this modification I made, .map((res:Response) => res.text())

  • now the parameters of the . map gave the following error: [ts] Argument of type '(res: Response) => Promise<string>' is not Assignable to Parameter of type '(value: Response, index: number) => Promise<string>'. Types of Parameters 'res' and 'value' are incompatible. Type 'import("/home/Marcelo/Desktop/Mercatorio/Dashboard/node_modules/@angular/http/src/static_response"). Response' is not Assignable to type 'Response'. Property 'redirected' is Missing in type 'Response'. [2345]

  • What version of angular? May have some change in the import of map, try like this : import { map } from "rxjs/operators";

  • Angular CLI: 7.0.4 Node: 8.12.0 OS: linux x64 Angular: 5.1.2, I’ve already imported the map

  • It should work, it seems that the Sponse that imported import("/home/marcelo/Desktop/Mercatorio/dashboard/node_modules/@angular/http/src/static_response") is not correct, tries to remove this import and leave the type as any

  • the permance error: [ts] Argument of type '(res: Response) => Promise<string>' is not Assignable to Parameter of type '(value: Response, index: number) => Promise<string>'. Types of Parameters 'res' and 'value' are incompatible. Type 'import("/home/Marcelo/Desktop/Mercatorio/Dashboard/node_modules/@angular/http/src/static_response"). Response' is not Assignable to type 'Response'. Property 'redirected' is Missing in type 'Response'. [2345] interface Response

  • Some light to give me?

  • I called you in the chat room.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.