Decode function

I am trying to download a file via a API. I have a problem decoding the base64 stream. When a use the decode function a export the file (MS Word and PDF) de files are corrupt. The PDF file can be opend but has no tekst and the DOCX file is corrupt accoding to MS WORD.
(I tried de BASE64 and BASE64URL paramater)

Decode with Powershell works, but a hope EM could do it.

Hi Jan,

How do you do it with PowerShell? Can you post the script?

The script I use:

Stel het pad naar het base64 bron en doelbestand in

$bronBestand = "\gn.karelstad.nl\Documenten\sandj1\My Documents\Handtekening outlook voorbeeld BASE64.docx"
$doelBestand = $bronBestand.replace(" BASE64","")

$base64String = Get-Content -Path $bronbestand -Raw
$decodedBytes = [Convert]::FromBase64String($base64String)

Save the decoded data to the output file

[IO.File]::WriteAllBytes($doelBestand, $decodedBytes)

Write-Host "Decoded data saved to $doelBestand"

Hi, today I've same issue... my EasyMorph decoded Base64 string was 530 byte VS 834 Kb of entire Base64 original file...

Could you share more information about what you are trying to achieve, what kind of file is encoded, and how it is encoded? The decode function expects the text argument to be base64-encoded UTF-8 text - not UTF-32 or other encodings.

Hi Vlad,
in the attach an example:

  • Input Pdf.pdf --> my base
  • Base64.dset --> it contains the previous file converted in Base64 via Python script
  • Test Pdf Base64.morph --> I simulated as if the web service returns me the Base64 of a PDF, I convert it with the EM function, I save the file and I would expect the generated PDF to open
  • Base64.pdf --> the file generated by EM... but which does not open because it was not decoded correctly

However, if I convert the PDF with an online tool, I can get the PDF back... I was wondering if further processing is necessary with EasyMorph to get what I want.

Test Pdf Base64.zip (422.4 KB)

Thank you for sharing the example.
There are a few issues. First - decode is specifically for decoding UTF-8 text into a UTF-16 string (EasyMorph text cell), so when trying to decode binary bytes (PDF) it would produce a string with a bunch of invalid UTF symbols.


So even if you simply do InputBase64 -> decode -> encode as Base64 roundtrip, the result would not be equal to the input base64. One way to solve it would be to use a PowerShell action with something like [IO.File]::WriteAllBytes("Q:/output1.pdf", [Convert]::FromBase64String($input)):

I tested it with your sample Base64 dataset and it seems to produce a correct PDF.

Also, I see that you export the file as a dataset. Even if the extension is *.pdf, it still means that it is written in EasyMorph's file format - meaning it has all the headers, metadata, column info, and so on, so you wouldn't be able to open it as a PDF this way unfortunately, even if decode worked with binary data.

Ok thanks for your help, it works!

To help anyone else who might need the procedure, I'll show you how I wrote the PowerShell code:

I rearranged the code to iterate through my Base64 blobs and call the module with PowerShell, where I have three parameters:

  • Filename --> what I want the file to be called
  • Content --> the base64 file
  • LocalPath --> the path where I want the file to be saved

$folder = "{LocalPath}"
$filename = "{Filename}"
$base64 = "{Content}"

$fullpath = Join-Path $folder $filename

$bytes = [System.Convert]::FromBase64String($base64)
[System.IO.File]::WriteAllBytes($fullpath, $bytes)

[PSCustomObject]@{
path = $fullpath
status = "Creato"
}