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"));
});