Angular 6 无法在 HttpClient 错误消息中读取八位字节流:“解析...

Man*_*n T 1 typescript angular angular-httpclient

我想使用 angular 6 HttpClient (@angular/common/http) 向服务器发出非 json 请求以获取 Octet-stream 。下面是我的代码。

getFile(file: any) {
    let headers: new HttpHeaders({
        'Content-Type':  'application/octet-stream',
        'Accept':'application/octet-stream',
        'Authorization': 'Bearer ' + data.token
    })

    return this.http.get<any>(this.baseUrl + '/getfile/'+file, headers);
}
Run Code Online (Sandbox Code Playgroud)

但这给了我 JSON 解析错误。

"HttpErrorResponse", message: "Http failure during paring for..... ERROR {...} ? error: {...} ?? error: SyntaxError: "JSON.parse: JSON 数据第 1 行第 1 列的意外字符"

将其读取为非 json 的方法是什么?

Dav*_*vid 6

尝试这个:

let headers = new HttpHeaders({
    'Authorization': 'Bearer ' + data.token
    });
return this.http.get<any>(this.baseUrl + '/getfile/'+file, {headers:headers, responseType: 'blob'});
Run Code Online (Sandbox Code Playgroud)