下载PDF作为Blob在Chrome iOS上不起作用
在Angular 7应用程序中,我有以下用于在各种平台上下载PDF的代码下载PDF作为Blob在Chrome iOS上不起作用,ios,angular,google-chrome,pdf-generation,chrome-ios,Ios,Angular,Google Chrome,Pdf Generation,Chrome Ios,在Angular 7应用程序中,我有以下用于在各种平台上下载PDF的代码 this.http.get('/api/url', {responseType: 'blob'}).pipe(map(res => { return { filename: 'filename.pdf', data: res }; })) .subscribe( res => { const file
this.http.get('/api/url', {responseType: 'blob'}).pipe(map(res => {
return {
filename: 'filename.pdf',
data: res
};
}))
.subscribe(
res => {
const fileBlob = new Blob([res.data], {type: 'application/pdf'});
if (navigator && navigator.msSaveBlob) { // IE10+
navigator.msSaveBlob(fileBlob, res.filename);
} else if (navigator.userAgent.match('CriOS')) { // iOS Chrome
const reader = new FileReader();
reader.onloadend = () => {
window.location.href = reader.result.toString();
};
reader.readAsDataURL(fileBlob);
} else if (navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPhone/i)) { // iOS Safari and Opera
const url: string = URL.createObjectURL(fileBlob);
window.location.href = url;
} else {
const url: string = URL.createObjectURL(fileBlob);
const a: any = document.createElement('a');
document.body.appendChild(a);
a.setAttribute('style', 'display: none');
a.href = url;
a.download = res.filename;
a.click();
URL.revokeObjectURL(url);
a.remove();
}
}
);
除了Chrome iOS,下载在所有平台上都可以正常工作。我主要关注和其他一些类似的链接
我还为Chrome iOS尝试了以下案例
const reader = new FileReader();
reader.onloadend = () => {
window.open(reader.result.toString());
};
reader.readAsDataURL(fileBlob);
还将onloadend替换为上面的onload,并尝试了两种方法。
此外,我还尝试了Safari的代码,但也失败了
知道我在这里遗漏了什么吗?删除自定义chrome ios检查对我来说很有效:
...subscribe(response => {
const file = new Blob([response.body], {type: 'application/pdf'});
const fileURL = (window.URL || window['webkitURL']).createObjectURL(file);
const fileName = 'Whatever.pdf';
const downloadLink = document.createElement('a');
downloadLink.href = fileURL;
downloadLink.download = fileName;
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
})