File to Base64 with JavaScript

To convert a String to/from Base64 in JavaScript, we use the btoa and atob commands.

Here is another code to convert a file to Base64:

ToBase64.htm

<script>
async function readTextFileAsBlob(file) {
    const response = await fetch(file);
    const blob = await response.blob();
    return blob;
}

const blobToBase64DataURL = blob => new Promise(  
    resolvePromise => {
        const reader = new FileReader();
        reader.onload = () => resolvePromise(reader.result);
        reader.readAsDataURL(blob);
    }
);

my_file = AppInventor.getWebViewString(); // INPUT
readTextFileAsBlob(my_file).then(
    async blob => {
        const base64URL = await blobToBase64DataURL(blob);
	 //	  document.write(base64URL);
	      AppInventor.setWebViewString(base64URL); // OUTPUT
    }
);
</script>

p70_base64_javascript.aia (11.4 KB)

base64javascript1

This has promise :slight_smile:
Does the readTextFileAsBlob function also work for other file types, e.g. image, pdf, zip?

I think so, in the app I have put a .png file.

Now trying to return base64 to readable string wit atob.

my_file = AppInventor.getWebViewString(); // INPUT
readTextFileAsBlob(my_file).then(
    async blob => {
        const base64URL = await blobToBase64DataURL(blob);
	 //	  document.write(base64URL);
	  // AppInventor.setWebViewString(base64URL); // OUTPUT
	  var my_file2 =  atob(base64URL.substring(23));
      AppInventor.setWebViewString(my_file2); // OUTPUT
    }
);

1.- Image in asset. JavaScript encode image ToBase64 String. Return String. String to Files by Canvas Block and extension. Files in ASD.

p70_base64_javascript_v3.aia (33.9 KB)

ToBase64.htm

<script>
async function readTextFileAsBlob(file) {
    const response = await fetch(file);
    const blob = await response.blob();
    return blob;
}

const blobToBase64DataURL = blob => new Promise(  
    resolvePromise => {
        const reader = new FileReader();
        reader.onload = () => resolvePromise(reader.result);
        reader.readAsDataURL(blob);
    }
);

my_file = AppInventor.getWebViewString(); // INPUT
readTextFileAsBlob(my_file).then(
    async blob => {
        const base64URL = await blobToBase64DataURL(blob);
               // .split("base64,")[1]) to remove data:image/png;base64,
	     AppInventor.setWebViewString(base64URL.split("base64,")[1]); // OUTPUT    
    }
);
</script>
1 Like

Try using UriEncode block available in Web component.

Look at @Juan_Antonio 's last example above

failed to save, bad base64 when saving using canvas.

image

please understand my question, that:
the data I want to save is a blob that has been converted to base64 with an example url is:

data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,UEsDBAoAAAAAADMe2laWJXaW0QEAANEBAAATAAAAW0NvbnRlbnRfVHlwZXNdLnhtbDw/eG1sIHZlcnNpb249IjEuMCIgZW5jb2Rpbmc9IlVURi04IiBzdGFuZGFsb25lPSJ5ZXMiPz4KPFR5cGVzIHhtbG5zPSJodHRwOi8vc2NoZW1hcy5vcGVueG1sZm9ybWF0cy5vcmcvcGFja2FnZS8yMDA2L2NvbnRlbnQtdHlwZXMiPgogIDxEZWZhdWx0IEV4dGVuc2lvbj0icmVscyIgQ29udGVudFR5cGU9CiAgICAiYXBwbGljYXRpb24vdm5kLm9wZW54bWxmb3JtYXRzLXBhY2thZ2UucmVsYXRpb25zaGlwcyt4bWwiIC8+CiAgPE92ZXJyaWRlIFBhcnROYW1lPSIvd29yZC9kb2N1bWVudC54bWwiIENvbnRlbnRUeXBlPQogICAgImFwcGxpY2F0aW9uL3ZuZC5vcGVueG1sZm9ybWF0cy1vZmZpY2Vkb2N1bWVudC53b3JkcHJvY2Vzc2luZ21sLmRvY3VtZW50Lm1haW4reG1sIi8+CiAgPE92ZXJyaWRlIFBhcnROYW1lPSIvd29yZC9hZmNodW5rLm1odCIgQ29udGVudFR5cGU9Im1lc3NhZ2UvcmZjODIyIi8+CjwvVHlwZXM+ClBLAwQKAAAAAAAzHtpWAAAAAAAAAAAAAAAABgAAAF9yZWxzL1BLAwQKAAAAAAAzHtpWD8Iv5UoBAABKAQAACwAAAF9yZWxzLy5yZWxzPD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9InllcyI/Pgo8UmVsYXRpb25zaGlwcyB4bWxucz0iaHR0cDovL3NjaGVtYXMub3BlbnhtbGZvcm1hdHMub3JnL3BhY2thZ2UvMjAwNi9yZWxhdGlvbnNoaXBzIj4KICA8U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h8LDQAACw0AABAAAAAAAAAAAAAAAAAA8wsAAHdvcmQvYWZjaHVuay5taHRQSwECFAAKAAAAAAAzHtpWAAAAAAAAAAAAAAAACwAAAAAAAAAAABAAAAAsGQAAd29yZC9fcmVscy9QSwECFAAKAAAAAAAzHtpWWSFTmjIBAAAyAQAAHAAAAAAAAAAAAAAAAABVGQAAd29yZC9fcmVscy9kb2N1bWVudC54bWwucmVsc1BLBQYAAAAACAAIAOEBAADBGgAAAAA=

I have decoded it using javascript, but the decoded results are not as expected, where if the above link is opened using a windows web browser (chome) the results can be opened, but if opened using app inventor via webstring it cannot, as for the javascript file to open is as follows :

<script>
function decodeBase64BlobToText(base64Blob, encoding) {
  var binaryString = atob(base64Blob);
  var decoder = new TextDecoder(encoding);
  var decodedText = decoder.decode(new Uint8Array([...binaryString].map(char => char.charCodeAt(0))));
  return decodedText;
}

var base64Blob = AppInventor.getWebViewString(); // Base64-encoded blob yang akan didekodekan
const base64 = base64Blob.split(',')[1];
var encoding = 'windows-1252';
var decodedText = decodeBase64BlobToText(base64, encoding);
AppInventor.setWebViewString(decodedText);
</script>

above javascript I provide additional character encoding, but still failed to decode base64 above. Is it my fault from the decode, or is it true that Android can't decode base64 with full characters, because I open the decode file from the Chrome webbrowser, there are different characters from the decode results using the application + javascript

Please help

Is your example datauri just an empty word document ?

Why can you not just download the file ?

the file I want to download is the result of converting from json to docx done by javascript, therefore it starts with blob.

when I want to download it directly, an error message "only http/https" appears, or if not the error immediately closes the application, this is because the url that I want to download does not start with http or https, but starts with a blob

I was looking for references, it turns out that Android can't download using the download manager, so I tried the trick to change the blob to base64data again.

the point is, those who want to help, please copy the entire code section below, and paste it in the pc web browser url, it will download automatically;

Could you please answer my questions above ?

With this extension you can convert a Base64 String to a file in the ASD.

borrar_dcosx.aia (26.5 KB)