我尝试调用rest函数,但显示如下错误:
GET http://localhost:9000/api/filess/[object%20Object] 400 (Bad Request)
。
可能无法获得报表数据变量的名称。我怎么得到它?
我该如何解决这个问题?
下面的组件侧:
showReport(filename: string) {
this.reportData = new ReportData();
this.reportData.month=this.selectedMonth;
this.reportData.year= this.selectedYear;
this.reportData.fileName= filename;
var reportData: ReportData= this.reportData;
this.defReportService.downloadFile(reportData).subscribe(
res => {
// var file = new Blob([res], {type: 'application/pdf'});
var file = new Blob([res], { type: 'application/vnd.ms-excel' });
var fileURL = URL.createObjectURL(file);
// window.open(fileURL);
const fname = filename + '_' +
fileURL.toString().substr(fileURL.toString().lastIndexOf('/') + 1) + '.xls';
saveAs(res, fname);
},
error => {
console.log(error);
}
);
}
下面的服务端:
public downloadFile(req?: ReportData) {
const encodedAuth = window.localStorage.getItem('encodedAuth');
return this.http
.get(`${this.resourceDownload}/${req}`, {
headers: new HttpHeaders({
Authorization: 'Basic ' + encodedAuth,
'Content-Type': 'application/octet-stream'
}),
responseType: 'blob'
})
.pipe(
tap(
// Log the result or error
data => console.log('You received data'),
error => console.log(error)
)
);
}
REST下方:
@GetMapping("/filess/{reportData}")
@ResponseBody
public ResponseEntity<Resource> getFile(@PathVariable ReportData reportData) throws Exception {
EnumList.JRList reportEnum = EnumList.JRList.valueOf(reportData.getFileName());
File createdFile = jasperService.downloadReportFile(reportEnum, EnumList.JRExport.XLSX, reportData);
Resource resourceFile = jasperService.loadFile(createdFile.getPath());
String filePath = createdFile.getPath();
//createdFile.delete();
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + filePath + "\"")
.body(resourceFile);
}
答案 0 :(得分:1)
如果您要发送对象之类的复杂数据结构,最好使用http POST
方法
开机自检
const data = {...};
http.post(url , JSON.stringify(data));
如果使用GET方法,则可以使用params属性url?p=v&p2=v2
const options= { params: new HttpParams().set('name', ...) }
http.get(url , options)
将对象映射到httpParams
const p = { p1: "1", p2: "2", p3: "3" };
const params = Object.entries(p).reduce(
(data: HttpParams, par) => data.append(par[0], par[1]),
new HttpParams()
);
console.log(params.toString()); // => "p1=1&p2=2&p3=3"
http.get(url , {params})