How to download blob from WKWebView

I have developed an app that allows the user to validate himself on a website and, through a webview, navigate through it. In the event that the user tries to download a document through a link, it is stored on their device. The problem arises when the download url is of the type blob: // https: // .... To contemplate this case, I have tried to evaluate a javascript that allows obtaining the bytes of the document in base64 using the XMLHttpRequest object (analogously to how I have solved it in android), but in this case the onload method always the value 0 for the status attribute of the XMLHttpRequest object. Is there a solution to be able to download blob files from a WKWebView in swift?

Hi, I went through the same problem as you. Here is the JS Script with I use to solve this problem:

"""        (async function download() {                         javascript: var xhr = new XMLHttpRequest();                         xhr.open('GET', '(absoluteUrl)', true);                         xhr.setRequestHeader('Content-type','application/octet-stream');                         xhr.responseType = 'blob';                         xhr.send();                         xhr.onload = await function(e) {                             if (this.status == 200) {                                 var blobFile = this.response;                                 var reader = new FileReader();                                 reader.readAsDataURL(blobFile);                                 reader.onloadend = function() {                                     base64data = reader.result;                                     window.webkit.messageHandlers.getBase64FromBlobData.postMessage(base64data);                                 }                             }                         };                     })();                 """

I use this two links as reference:

I hope it help you

How to download blob from WKWebView
 
 
Q