我正在尝试下载一个pdf格式的API响应。API-1将返回文件名,并将文件名作为API - 2的输入,我将下载pdf。对于积极的案例,它工作得很好。如果API - 1没有返回fileName,我就不应该调用API-2,而是要告诉用户popupDialog中不存在任何文件。
this.pdf.pdfName(pdfInfo).pipe(
tap(res => fileName = res.fileName),
//Inside concatMap am not able to handle this condition (getVersionPdfFile-observable/printEmptyAlert - just a matdialog)
concatMap(res => !!res.fileName ? this.pdf.getVersionPdfFile(res.fileName) : this.printEmptyAlert())
).subscribe(fileResponse => {
var newBlob = new Blob([fileResponse], { type: "application/pdf" });
const data = window.URL.createObjectURL(newBlob);
var link = document.createElement('a');
link.href = data;
link.download = fileName;
link.click();
window.URL.revokeObjectURL(data);
});
发布于 2021-08-18 05:01:50
当没有文件名时,您可以抛出错误(使用throwError
),并在错误块中处理该错误:
导入throwError
import { throwError } from 'rxjs';
this.pdf.pdfName(pdfInfo).pipe(
tap(res => fileName = res.fileName),
concatMap(res => !!res.fileName ? this.pdf.getVersionPdfFile(res.fileName) :
throwError('No file name'))
).subscribe(fileResponse => {
var newBlob = new Blob([fileResponse], { type: "application/pdf" });
const data = window.URL.createObjectURL(newBlob);
var link = document.createElement('a');
link.href = data;
link.download = fileName;
link.click();
window.URL.revokeObjectURL(data);
}, (error) => {
// Handle error here
if(error === 'No file name'){
this.printEmptyAlert();
}
});
https://stackoverflow.com/questions/68826674
复制相似问题