Xbasic

base64Decode Function

Syntax

DecodedData as B = base64Decode(C encode_string)

Arguments

encode_stringCharacter

A character string containing base64 encoded data. If your base64 string has a data prefix containing a mime type, you must remove it before calling this function.

Returns

DecodedDataBinary

The decoded base64 data.

Description

Converts a base64 encoded string back to its binary data. Can be used to convert base64 images back to their original format.

Discussion

The base64Decode() function returns binary data decoded from a base64 encoded character string.

This function can be used to convert data into it's original file format, which can then be saved as a file to storage. For example, the Xbasic script below converts a base64 encoded png into it's original binary data and saves it to disk using storage:

dim img64 as c = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
dim decodedImg as b
decodedImg = base64decode(img64)

dim filename as c = "mydata.png"
dim mime as c = "image/png"
dim result as P
a5storage_savedata("::storage::disk",decodedImg,filename,mime,result)

? result
= AbsolutePath = "file://C:\Users\user1\Documents\AlphaAnywhere\Demo\Demo.WebProjects\Default.WebProject\images\mydata.png"
ContentType = "image/x-png"
hasError = .F.
ModifiedTime = 05/06/2021 12:29:25 64 pm
Name = "mydata.png"
size = 85
timeTakenMilliseconds = 5

See Also