2016-10-23 4 views
2

受信したファイルは$httpになります。応答データをファイルに保存する必要があります。angular-file-saverを使用しています。受信したファイルはpdf docxですが、保存されたPDFファイルは読者に何も表示されませんが、PostManのような他のダウンローダはそれを正しく保存します。Angularjs:サーバーから受け取ったblob pdfファイルを正しく保存する方法

function saveFile(data, header){ 
    var extensions = { 
     "application/pdf": ".pdf", 
     "application/vnd.openxmlformats-officedocument.wordprocessingml.document": ".docx" 
    } 

    var file = new Blob([data], {type: header}); 

    FileSaver.saveAs(file, documentsShow.document.name + extensions[header]); 
} 

dataはボディ$http成功とheaderから取得したデータ、それが必要だ場合も、場合$http応答

から受け取っContent-Typeです:

省の世話をする関数は、このコードを持っています$httpコードは次のようになります。

$http({ 
    method: "GET", 
    data: data, //This is sometimes null but it's not relevant for the application 
    url: "path/to/download" 
}).then(function(data){ 
    saveFile(data.data, data.header("Content-Type")); 
}); 

答えて

3

主な問題ここでは、Angularが依然としてJSONとして応答を解析しようとしていることがわかります。それはFYI

$http.get('path/to/download', { 
    responseType: 'blob' 
}).then(function(res) { 
    FileSaver.saveAs(res.data, ...) 
}); 

Blobオブジェクトに解決しますので、あなたはこれを変更することができ

dataGET要求で使用されていません。

関連する問題