1
votes

I try to send an GET request from Angular to my Laravel backend.

    Route::get('excel', function () {
    return Excel::download(new PqrsExport, 'products.xlsx');
});

I subscribe to this method

get_excel(){
    this.pqrService.getExcel().subscribe(
      resp => console.log(resp),
      err => console.log(err)
      );
  }

And get this

HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "http://localhost:8000/excel", ok: false, …}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
status: 200
statusText: "OK"
url: "http://localhost:8000/excel"
ok: false
name: "HttpErrorResponse"
message: "Http failure during parsing for http://localhost:8000/excel"
error: {error: SyntaxError: Unexpected token P in JSON at position 0 at JSON.parse (<anonymous>) at XMLHtt…, text: "PKVGSPG�D�X�[Content_Types].xml�…heets/_rels/sheet1.xml.relsPK��"}
__proto__: HttpResponseBase
1
Angular expect response to be json encoded, you should return response()->json(Excel::download(new PqrsExport, 'products.xlsx')) but i don't think it will work like this, you should just send the pdf path to angular.lovis91
Where I put the return response()->json(Excel::download(new PqrsExport, 'products.xlsx')) ?Alezco05
instead of return Excel::download(new PqrsExport, 'products.xlsx');lovis91
It doesn't show the error now, but it also doesn't download anythingAlezco05
yeap that's normal behavior, what's the content of the response ?lovis91

1 Answers

1
votes

I solved it installed FileSaver

npm i file-saver

And use this way

HTML

 <button class="btn btn-success" (click)="get_excel()">Excel</button>

TS FILE

import * as FileSaver from 'file-saver';


 get_excel(){
    return this.http.get('http://localhost:8000/excel',  { responseType: 'blob' })
    .subscribe((resp: any) => {
      FileSaver.saveAs(resp, `filename.xlsx`)
    });
  }