首页 > 解决方案 > “HttpResponse”类型上不存在属性“blob”'

问题描述

我正在尝试将 Angular 4 升级到 5,为此我必须从 @angular/common/http 导入 HttpClient、HttpResponse、HttpHeaders,而不是从“@angular/http”导入 Http、Response、Headers、RequestOptions、ResponseContentType;

但在我之前的代码中,我使用的是 let headers = new Headers(); 我替换为 let headers = new HttpHeaders(); 和 responseType: ResponseContentType.Blob 替换为 responseType: 'blob'

现在在方法 getData() res.blob() 中说“HttpResponse”类型上不存在属性“blob”。

ExcelDownload(_getRequestUrl: string) {
            let headers = new HttpHeaders();
            headers.append('Content-Type', 'application/json' ); 
            return this.http.get(this.getUrlWithEmpId(_getRequestUrl), {
                headers : headers,
                responseType: 'blob'
        }).map(response => this.getData(response)) .catch(error => this.handleError(error));
        }   
        public getData(res: HttpResponse<any>) {
            let headers = res.headers;
            let contentType = headers.get('Content-Type');
            var blob;
            try {
                if(contentType === 'pdf' || contentType === 'application/pdf') {
                    //chek this
                    blob = new Blob([res.blob()], { type: "application/pdf"});
                } else {
                    //check thi
                    blob = new Blob([res.blob()], { type: "application/vnd.ms-excel"});
                }
            }
            catch (e) {
              
            }
            return blob;
        }

标签: angular5

解决方案


推荐阅读