如何从角度5的网址下载pdf文件

时间:2018-04-26 09:05:57

标签: angular5

我目前在这个问题上花了一天时间,仍然无法从角度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);
        }
    )
}

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

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

如何解决这个问题。任何人都可以发布正确的代码从角度5的网址下载pdf文件?

2 个答案:

答案 0 :(得分:10)

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

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

答案 1 :(得分:1)

尝试一下

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

参考文献: