Skip to main content

Notifications

Small and medium business | Business Central, N...
Suggested answer

Convert BLOB to Base64 for use in Web Service

Posted on by 10

Hello, I've been trying, and failing miserably, to convert a BLOB data type to and Base64 text string in order to send it through a web service.

Here's what I tried:

In both cases, I get this error:

pastedimage1679921691099v1.png

I tried to set the length of the string to 2048(max value), and then it tried to assign 39,000 characters. I will appreciate any help I can get.

I'm working in BC14.

  • Suggested answer
    Greg Enns Profile Picture
    Greg Enns 1,102 on at
    RE: Convert BLOB to Base64 for use in Web Service

    Did you try using the Base64 conversion functions in Codeunit 10 "Type Helper"?

    There is both ConvertValueFromBase64 and ConvertValueToBase64.

  • Jesse Otu Profile Picture
    Jesse Otu 10 on at
    RE: Convert BLOB to Base64 for use in Web Service
    [quote user="Nitin Verma"]

    In which line you are facing the issue, also you can avoid showing the message and direct your URL in here.

    [/quote]

    The issue is in the codeunits where the data type conversion is happening.

    This is a link to codeunit 4110 and 4111.

    github.com/.../Base64Convert

    pastedimage1679997675008v1.png

    The error is thrown after the line Base64String := Convert.ToBase64String(InputArray).

    This method uses the dotnet Convert subtype to run the conversion.

    I hope this answers your question. The other linked codeunit uses a different implementation but still throws the exact same error.

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: Convert BLOB to Base64 for use in Web Service

    In which line you are facing the issue, also you can avoid showing the message and direct your URL in here.

  • Jesse Otu Profile Picture
    Jesse Otu 10 on at
    RE: Convert BLOB to Base64 for use in Web Service

    Okay Nitin Verma here you go:

    OnRun()
    GetEmployeeImage3('JR');

    GetEmployeeImage3(EmployeeCode : Code[20]) : Text[2048]
    Employees.GET(EmployeeCode);
    IF NOT Employees.Image.HASVALUE THEN
    EXIT;

    TenantMedia.GET(Employees.Image.MEDIAID);
    TenantMedia.CALCFIELDS(Content);
    IF TenantMedia.Content.HASVALUE THEN BEGIN

    CLEAR(PictureText);
    CLEAR(PictureInStream);
    TenantMedia.Content.CREATEINSTREAM(PictureInStream, (TEXTENCODING::UTF8));
    //PictureText.ADDTEXT(Base64Convert.StreamToBase64String(PictureInStream));
    //PicDataBASE64 := Base64Convert.StreamToBase64String(PictureInStream);
    //PicDataBASE64 := Base64Convert1.ToBase64_6(PictureInStream);
    PictureText.ADDTEXT(Base64Convert1.ToBase64_6(PictureInStream));
    MESSAGE(PicDataBASE64);

    END;

    This is my codeunit where I tried using both resources referenced in my original post.

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: Convert BLOB to Base64 for use in Web Service

    is it possible to share the code

  • Jesse Otu Profile Picture
    Jesse Otu 10 on at
    RE: Convert BLOB to Base64 for use in Web Service

    I'm working in BC 14. I've updated my post accordingly.

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: Convert BLOB to Base64 in Dynamics NAV for use in Web Service

    Hi,

    Are you trying to do in NAV or BC?

    Thanks.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

New! Quick response templatesâš¡

Save time with the new custom templates!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,219 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,056 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans