Back to all How-tos Decode base64 images to a binary file This script will take the current data file which is assumed to be a base64 string of an image type. Useful if you have data coming from the COTG mobile application and need to obtain a copy of a submitted image! Convert job file from base64 to binary // This section loads the contents of the base64 string. The active example // loads the current job file. var fso = new ActiveXObject("Scripting.FileSystemObject"); var source_file = fso.OpenTextFile(Watch.GetJobFilename(), 1); var base64_contents = source_file.ReadAll(); // Then we call the function below. This setup writes back to // the current job file, but it can be a path on the disk (second parameter) // The third parameter determines the mime type, which can be image/jpg, image/png, etc. // It has to correspond to what's present in the base64 string! base64ToBinary(base64_contents, Watch.GetJobFilename(), "image/jpeg"); // This is the magic function that "cheats" by writing the // base64 string directly into a binary node. MAGIC FTW. // This is extremely fast and efficient, by the way. function base64ToBinary(base64String, filePath, mimeType) { var dom = new ActiveXObject('Microsoft.XMLDOM'); var elem = dom.createElement('tmp'); elem.dataType = 'bin.base64'; base64String = base64String.replace("data:"+mimeType+";base64,", ""); elem.text = base64String; var decodeBase64 = elem.nodeTypedValue var inputStream = new ActiveXObject('ADODB.Stream'); inputStream.Open(); inputStream.Type = 1; // adTypeBinary inputStream.Write(decodeBase64); inputStream.SaveToFile(filePath, 2); } Getting the string from another source: Here are some other sources you can use to obtain the base64 data: // Load a local variable's content as the base64 string: // var base64_contents = Watch.GetVariable("encodedImage"); // // Load the data from the XML envelope of the HTTP Server Input: // var base64_contents = Watch.ExpandString("xmlget('/request[1]/values[1]/photo-data[1]',Value,KeepCase,NoTrim)"); VBScript version The same trick can be used in VBScript, using pretty much the same “xml binary node” cheat as above: Dim objFSO, objFile, strBase64 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(Watch.GetJobFileName, 1) strBase64 = objFile.ReadAll objFile.Close byte64ToJpeg strBase64, Watch.GetJobFileName Private Sub byte64ToJpeg(byte64string,jpegpath) dim DM, EL,decodeBase64 Set DM = CreateObject("Microsoft.XMLDOM") Set EL = DM.createElement("tmp") EL.DataType = "bin.base64" EL.Text = Replace(byte64string,"data:image/jpeg;base64,", "") decodeBase64 = EL.NodeTypedValue Dim binaryStream Set binaryStream = CreateObject("ADODB.Stream") binaryStream.Type = 1 binaryStream.Open binaryStream.Write decodeBase64 binaryStream.SaveToFile jpegpath, 2 set EL = nothing set DM = nothing set binaryStream = Nothing end sub Tags samples scripts workflow Leave a Reply Cancel reply Your email address will not be published. Required fields are marked *Cancel