I've a file in AWS S3 bucket. And from react app I'm generating the presigned url to download it to the user. I didn't used fetch(), axios.get() or https.get() because I do not want to hold the file content in the memory as we may need to process many such requests at the same time and the file size may also be huge.
So we have decided to use window.location = presignedurl
so that the user will have a feel like the file is getting downloaded from the same page.
And this is working fine for successful download. But when there is an issue with the presigned url like, nosuchkey
, timedout
, signaturedoesnotmatch
errors. These errors are thrown at the UI and not able to catch such errors as our code is not in the scope.
So I have decided to do this inside a hidden iframe so that whenever there is an error, that error will be present only in hidden iframe and the user will not see it. But to catch that error and log it, I tried to access that from the parent frame. I know this is not possible due to security reasons. So I tried window.postMessage() from inside iframe to pass the content to parent.
But the problem is, as I'm generating this iframe dynamically using document.createElement('iframe')
and defined onload function of iframe as iframe.onload = function() {}
, I'm not able to get the content out of it. Whenever I use iframe.contentDocument || iframe.contentWindow.document
again I'm getting cross-origin issue.
Is there a way to get the error message? like, is there a way to check the url has the valid file before downloading it from client browser?