如何从 angular 5 的 url 下载 pdf 文件 [英] how to download a pdf file from an url in angular 5

查看:42
本文介绍了如何从 angular 5 的 url 下载 pdf 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在这个问题上花了一天时间,仍然无法从 angular 5 的 url 下载文件

I currently spend one day in this issue,still failed to download a file from an url in angular 5

leadGenSubmit() {

    return this.http.get('http://kmmc.in/wp-content/uploads/2014/01/lesson2.pdf',
    {responseType:ResponseContentType.Blob}).subscribe((data)=>{
        console.log(data);
        var blob = new Blob([data], {type: 'application/pdf'});
        console.log(blob);
        saveAs(blob, "testData.pdf");
    },
    err=>{
        console.log(err);
        }
    )
}

当我运行上面的代码时,它显示以下错误

when I run above code it shows following error

ERROR TypeError: req.responseType.toLowerCase is not a function
    at Observable.eval [as _subscribe] (http.js:2187)
    at Observable._trySubscribe (Observable.js:172)

我该如何解决这个问题.有人可以发布正确的代码以从 angular 5 的 url 下载 pdf 文件吗?

how can I solve this issue.Can any one post the correct code to download a pdf file from an url in angular 5?

推荐答案

我认为你应该像这样定义 header 和 responseType:

I think you should define header and responseType like this:

let headers = new HttpHeaders();
headers = headers.set('Accept', 'application/pdf');
return this.http.get(url, { headers: headers, responseType: 'blob' });

这篇关于如何从 angular 5 的 url 下载 pdf 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆