使用Angular从API下载pdf文件 [英] download pdf file from API using Angular
本文介绍了使用Angular从API下载pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Method: PUT
Endpoint :
/application1/file/{filetype}/transactionType {
"accountNumber":"344224433344333"
}
API将返回CSV/pdf/xls类型的文件
API will return file of type CSV/pdf/xls
问题:
如何在新标签页中打开或下载使用angularJs 1.5
我尝试了几种解决方案,但在IE中不起作用 https://codepen.io/waghanil87/pen/vZBozE
I have tried few solution but it doesnot works in IE https://codepen.io/waghanil87/pen/vZBozE
提前谢谢!
推荐答案
您需要将base64字符串转换为 blob
类型,然后将其卸载/打印
you need to convert the base64 string to blob
type and then donload/print it
function b64toBlob(b64Data,callback) {
var contentType = 'application/pdf'; // put your file type here
var sliceSize = 512;
b64Data = b64Data.replace(/^[^,]+,/, '');
b64Data = b64Data.replace(/\s/g, '');
var byteCharacters = window.atob(b64Data);
var byteArrays = [];
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
var slice = byteCharacters.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
blob = new Blob(byteArrays, {
type: contentType
});
callback(blob)
}
function downloadData(fileName,blob) {
var a = document.createElement("a");
var url = window.URL.createObjectURL(blob);
document.body.appendChild(a);
a.style = "display: none";
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
}
// call it like this
b64toBlob(base64string,function(data){
function downloadData('sample.pdf',data)
})
这篇关于使用Angular从API下载pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文