以角度2保存二进制文件

时间:2017-07-24 20:19:34

标签: angular

我正在尝试以角度2保存二进制文件。

我的代码如下所示

  this.headers = new Headers({ 'Content-Type': 'application/json,application/octet-stream'});
            return this.http.post(this.processExportActionUrl, {headers: this.headers,  responseType: ResponseContentType.Blob}).toPromise().then(
                response => this.processDownloadFile(response, analyticMapping.sSAssetName)

And Process download file is 
// Creates a blob from data and downloads the file to the browser
    private processDownloadFile( data: any, assetName: string ) {
         let blob = new Blob([data._body], { "type": 'application/octet-stream' });
        // Must detect if the browser is IE in order to download files on IE.
        if (window.navigator.msSaveOrOpenBlob) {
            window.navigator.msSaveOrOpenBlob(blob, assetName+".zip");
        } else {
            let a = document.createElement("a");
            document.body.appendChild(a);
         //   a.setAttribute('style', 'display: none');
            let someUrl = window.URL.createObjectURL(blob);
            a.href = someUrl;
            a.download = assetName + ".zip";
            a.click();
            window.URL.revokeObjectURL(someUrl);
        }
    }

文件已下载,但不是zip存档。我尝试了各种组合,例如ResponseContentType.BlobResponseContentType.ArrayBuffer。似乎没有工作。 谢谢你的回复。

0 个答案:

没有答案