我尝试从Angular向我的Laravel后端发送GET请求。
Route::get('excel', function () {
return Excel::download(new PqrsExport, 'products.xlsx');
});
我订阅了此方法
get_excel(){
this.pqrService.getExcel().subscribe(
resp => console.log(resp),
err => console.log(err)
);
}
得到这个
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
答案 0 :(得分:0)
我解决了它安装了 FileSaver
npm i file-saver
并使用这种方式
HTML
<button class="btn btn-success" (click)="get_excel()">Excel</button>
TS文件
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`)
});
}