2017-11-22 17 views
0

私は角度2でapi呼び出しの応答から'xlsx 'ファイルをダウンロードしようとしています。応答本体で、データをxml形式で取得しています。ダウンロード中ですが、ファイルを開いた後に破損したファイルのようなエラーが表示されます。以下のサンプルコードをご覧ください。 response._body角度2のAPIコールの応答で.xlsxファイルをダウンロードできませんか?

this.homeDetailViewService.downloadReport(JSON.stringify(requestBody)) 
      .subscribe((response) => { 
       if (response) { 
        console.log(response); 
        var contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; 
        let blob = new Blob([response._body],{type:contentType}); 
        let link = document.createElement('a'); 
        link.href = URL.createObjectURL(blob); 
        link.download = "report.xlsx"; 
        link.click(); 
       } 
      }, 
      (error) => { 


       let errorObj = JSON.parse(error); 
       // If theres no data then reset the model list and total 
       if (errorObj.status === "error" && errorObj.message.match(/Record not found/)) { 
        this.curData = null; 
       } 
       this.loadingAnimation = false;     // error path 
       this.handleError(error); 
      }, 
      () => {// onComplete           
      }); 

は、だから私は '.xlsxの' 形式にXML型のレスポンスボディをダウンロードする方法がわからない

enter image description here

の下のようなデータを取得しています。

答えて

0

現在作業中です。

私はresponseType: ResponseContentType.Blobをリクエストオブジェクトに追加しました。

サービスファイルにResponseContentTypeをインポートしてから使用してください。

import { ResponseContentType } from '@angular/http'; 

この後、「ファイルの破損エラー」が表示され、ファイルデータが正常に出力されます。

関連する問題