Javascript 角度上的八位字节流-文件下载不工作
我正在尝试下载一个文件,点击一个按钮,api已经被成功点击,我得到了200个响应,但文件并没有被下载。它正在为邮递员工作 我的代码如下所示Javascript 角度上的八位字节流-文件下载不工作,javascript,angular,angular7,Javascript,Angular,Angular7,我正在尝试下载一个文件,点击一个按钮,api已经被成功点击,我得到了200个响应,但文件并没有被下载。它正在为邮递员工作 我的代码如下所示 /****单击*/ 下载资产(){ this.detailsService.getCsvReport(this.jobId.subscribe)(数据=>{}); } /****服务*/ getCsvReport(jobId):可观察{ const header={Accept:“应用程序/八位字节流”}; let endpoint:string=`${E
/****单击*/
下载资产(){
this.detailsService.getCsvReport(this.jobId.subscribe)(数据=>{});
}
/****服务*/
getCsvReport(jobId):可观察{
const header={Accept:“应用程序/八位字节流”};
let endpoint:string=`${Endpoints.REPORT}jobs/${jobId}/filePath?`;
返回此.get(端点,头).pipe(
地图(报告=>{
return report.data.parentXml;
})
);
}
下载报告
我认为您需要设置请求的响应类型
http.post的一个示例:
this.http.post(url + "/getFile", params, { responseType: "blob" }).subscribe(...)
希望我能提供帮助。我认为您需要设置请求的响应类型 http.post的一个示例:
this.http.post(url + "/getFile", params, { responseType: "blob" }).subscribe(...)
希望我能帮忙。你可以这样试试
getCsvReport(jobId): Observable<Object> {
const header = { Accept: "application/octet-stream" };
let endpoint: string = `${Endpoints.REPORT}jobs/${jobId}/filePath?`;
return this.get(endpoint, header).pipe(
map(report => {
const a = document.createElement('a');
document.body.appendChild(a);
const blob: any = new Blob([report.data.parentXml], { type: 'octet/stream' });
const url = window.URL.createObjectURL(blob);
a.href = url;
a.download = data.fileName;
a.click();
window.URL.revokeObjectURL(url);
})
);
}
getCsvReport(jobId):可观察{
const header={Accept:“应用程序/八位字节流”};
let endpoint:string=`${Endpoints.REPORT}jobs/${jobId}/filePath?`;
返回此.get(端点,头).pipe(
地图(报告=>{
常量a=document.createElement('a');
文件.正文.附件(a);
const blob:any=new blob([report.data.parentXml],{type:'octet/stream'});
constURL=window.url.createObjectURL(blob);
a、 href=url;
a、 下载=data.fileName;
a、 单击();
window.URL.revokeObjectURL(URL);
})
);
}
你可以这样试试
getCsvReport(jobId): Observable<Object> {
const header = { Accept: "application/octet-stream" };
let endpoint: string = `${Endpoints.REPORT}jobs/${jobId}/filePath?`;
return this.get(endpoint, header).pipe(
map(report => {
const a = document.createElement('a');
document.body.appendChild(a);
const blob: any = new Blob([report.data.parentXml], { type: 'octet/stream' });
const url = window.URL.createObjectURL(blob);
a.href = url;
a.download = data.fileName;
a.click();
window.URL.revokeObjectURL(url);
})
);
}
getCsvReport(jobId):可观察{
const header={Accept:“应用程序/八位字节流”};
let endpoint:string=`${Endpoints.REPORT}jobs/${jobId}/filePath?`;
返回此.get(端点,头).pipe(
地图(报告=>{
常量a=document.createElement('a');
文件.正文.附件(a);
const blob:any=new blob([report.data.parentXml],{type:'octet/stream'});
constURL=window.url.createObjectURL(blob);
a、 href=url;
a、 下载=data.fileName;
a、 单击();
window.URL.revokeObjectURL(URL);
})
);
}
似乎您获得了数据但没有触发文件下载是的,为什么,有什么建议吗?似乎您获得了数据但没有触发文件下载是的,为什么,有什么建议吗?此数据是什么。文件名?正在下载的文件名:)您可以设置动态或静态此数据是什么。文件名?正在下载的文件名:)您可以设置动态或静态