I am working on updating my webviewextra extension to be able to handle blob downloads from html files NOT on servers e.g. html files in assets. I use a javascript interface to convert a bloburl (blob) to base64. This currently works well (when the target html is running from a server that handles xhr) using this code:
public String GetBase64StringFromBlobUrl(String blobUrl, String mimeType) {
return "javascript: var xhr = new XMLHttpRequest();" +
"xhr.open('GET', '" + blobUrl + "', true);" +
"xhr.setRequestHeader('Content-type','" + mimeType + ";charset=UTF-8');" +
"xhr.responseType = 'blob';" +
"xhr.onload = function(e) {" +
" if (this.status == 200) {" +
" var blobFile = this.response;" +
" var reader = new FileReader();" +
" reader.readAsDataURL(blobFile);" +
" reader.onloadend = function() {" +
" base64data = reader.result;" +
" window.Android.getBase64FromBlobData(base64data);" +
" }" +
" }" +
"};" +
"xhr.send();";
}
I am attempting to replace this with different code:
public String GetBase64StringFromBlobUrl(String blobUrl, String mimeType) {
return "javascript:
"blobUrlToBase64(" + blobUrl + ").then(base64 => {" +
"window.Android.getBase64FromBlobData(base64);" +
"});" +
"async function blobUrlToBase64(bU) {" +
" try {" +
" const response = await fetch(bU);" +
" const blob = await response.blob();" +
" return new Promise((resolve, reject) => {" +
" const reader = new FileReader();" +
" reader.onloadend = () => {" +
" const base64 = reader.result.split(',')[1];" +
" resolve(base64);" +
" };" +
" reader.onerror = reject;" +
" reader.readAsDataURL(blob);" +
" });" +
" } catch (error) {" +
" reject(error);" +
" }" +
"}";
}
but this is not appearing to generate the base64 string for the next part? I don't believe I need the mimetype in the second code. ( I haven't removed it to avoid having to change things elsewhere)
This second javascript works just fine in an html file from the assets on AppInventor(companion)
Can anyone see what I might be missing?
I can provide further parts of the java code if needed.