Skip to content

Base64Encode an image string works in Windows but not in Mac #133

Open
@mmustala

Description

@mmustala

I tried to use the Base64Encoding function for a string containing image file bytes.

file = FreeFile()
Open ThisWorkbook.Path & dirSeparator & "temp_receipt.jpg" For Binary As #file
    imageString = Input(LOF(1), #file)
Close #file
encoded = Base64Encode(imageString)

This works in Windows but not in Mac.

In Mac the base64 encoding is done with these lines in the WebHelpers

web_Command = "printf " & PrepareTextForShell(Text) & " | openssl base64"
Base64Encode = ExecuteInShell(web_Command).Output

I'm not sure if the reason for the failure is in the PrepareTextForShell method or in the way how the data is given to the command line.

Luckily the #If Mac and ExecuteInShell works also outside of the VBA-Web files and I was able to add my own encoding logic for the Mac.

#If Mac Then
    Dim web_Command As String
    web_Command = "cat /path/to/image | openssl base64"
    encoded = ExecuteInShell(web_Command).Output
#Else
    file = FreeFile()
    Open image_path For Binary As #file
        imageString = Input(LOF(1), #file)
    Close #file
    encoded = Base64Encode(imageString)
#End If

I'm not sure if a fix for this issue should be made by modifying the existing Base64Encode method so that it can accept strings containing file contents or should it be fixed by adding a new method Base64EncodeFile.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions