[英]angular 2/4 handle res.download
I ran into a problem with downloading a file with angular. 我在下载角度文件时遇到了问题。
When using Postman the File shows correctly in the body. 使用Postman时,文件会正确显示在正文中。
I am using the express function res.download
and I can get the file to download but it is not a working png what am I doing wrong? 我正在使用快递功能res.download
,我可以下载文件,但不是有效的png我在做什么错?
My Angular 4 implimentation: 我的Angular 4实现:
this.http.post(localhost,{"jabcontent": cert._id})
.subscribe(
data => {
console.log(data.text());
var blob = new Blob([data.text()], {type: "image/png"});
FileSaver.saveAs(blob, "test.png" );
/* var blob = new Blob([data.text()], {type: "image/png"});
var objectUrl = URL.createObjectURL(blob);
window.open(objectUrl); */
}
)
I figured it out. 我想到了。
My backend was correct: 我的后端是正确的:
res.download(pathtoFile, filename, (err) => {
//err handeling
});
I did a mistake with the handeling of the respons. 我对回复的处理有误。 The mapping of the response was not correct also i set the response type to blob. 响应的映射也不正确,我也将响应类型设置为blob。 The switch to get was for consistency sake. 出于一致性考虑,转而使用。
var headers = new Headers();
headers.append('xyz', '1234'); //request parameter
let options = new RequestOptions({ headers: headers, responseType: ResponseContentType.Blob });
this.http.get('apipath', options)
.map((response: Response)=> response.blob())
.subscribe(data => {
FileSaver.saveAs(data, "filename");
}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.