Here is the code snippet that I've written to set the src attribute of an iframe to a blob URL:
function initiateDownloadFromIframe(fileData: any) {
const iframe = document.createElement('iframe');
iframe.setAttribute('style', 'display: none;');
const newBlobUrl = window.URL.createObjectURL(new Blob([fileData.blob], {type: 'application/pdf'}));
iframe.setAttribute('src', newBlobUrl);
const checkForDownload = () => {
if (iframe.contentWindow) {
setTimeout(function() {
// Remove iframe after 1 minute, assuming download has started
iframe.parentNode!.removeChild(iframe);
}, 60000);
} else {
setTimeout(checkForDownload, 100);
}
};
checkForDownload();
document.body.appendChild(iframe);
}
I want the iframe to automatically trigger the download of the PDF contained in the blob. However, at present nothing happens when this function is called.