Angular 5 HttpClient破坏了二进制下载

时间:2018-01-23 20:31:45

标签: angular5

使用angular 5 http客户端从删除服务器下载二进制文件时,文件一直被破坏。我一直无法找到与此版本客户端相关的资源来解决此问题。将响应类型设置为“arraybuffer”或“blob”不能解决问题。

在Angular 5中下载二进制文件并将其转换为Uint8Array的正确方法是什么?

我目前的代码如下

this.data = this.http.get(_.get(record, 'href', ''), {
                observe: 'events',
                reportProgress: true,
                responseType: 'blob'
            }))
        .do(httpEvent => {
            if (httpEvent.type === HttpEventType.DownloadProgress) {
                this.percent.next((httpEvent.loaded / httpEvent.total) * 100);
            }
        })
        .switchMap(httpEvent => {
            if (httpEvent.type === HttpEventType.Response) {
                return this.blobToArrayBuffer(httpEvent.body);
            }

            return Observable.of(new ArrayBuffer(0));
        })
        .map(buffer => {
            const buff = new Uint8Array(buffer);
            ... // Decode buffer here
        }


...

private blobToArrayBuffer(blob: Blob): Observable<ArrayBuffer> {
    return Observable.create(obs => {
        const fileReader = new FileReader();
        fileReader.onload = () => obs.next(fileReader.result);
        fileReader.onloadend = () => obs.complete();

        fileReader.readAsArrayBuffer(blob);
    });
}

我没有看到任何明显错误的响应标题

HTTP/1.1 200 OK
x-amz-id-2: ...
x-amz-request-id: ...
Date: Tue, 23 Jan 2018 18:25:07 GMT
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET
Access-Control-Max-Age: 3000
Vary: Origin, Access-Control-Request-Headers, Access-Control-Request-Method
Last-Modified: Thu, 18 Jan 2018 22:25:33 GMT
Server: AmazonS3
ETag: ...
Accept-Ranges: bytes
Content-Type: binary/octet-stream
Content-Length: 148341

任何见解都会有所帮助。

0 个答案:

没有答案