Thursday, November 11, 2021

Import Base 64 Data to BLOB Field in Business Central

 Hello Friends,

To convert base 64 string data to blob field.

Process blob field data to xmlport.

=======

We create the codeunit function to process base 64 data.

Below code will import Base 64 data to Blob field

  procedure FillBase64(Base64Text_iTxt: Text)
    var
        ImportLog_lRec: Record "Import Log Detail";
        VarOutStream: OutStream;
        Base64CU: Codeunit "Base64 Convert";
    begin
        ImportLog_lRec.Init();
        ImportLog_lRec.Insert();
        ImportLog_lRec.CalcFields("File Data");
        ImportLog_lRec."File Data".CreateOutStream(VarOutStream);
        Base64CU.FromBase64(Base64Text_iTxt, VarOutStream);
        ImportLog_lRec.Modify();
    end;

Call webservice function to pass base 64 data in function


























below code will process base 64 data (text import data) and process in xmlport import

CalcFields("File Data");
"File Data".CreateInStream(Instr);

Clear(ImportiMIS_xmlPort);
ImportiMIS_xmlPort.SetSource(Instr);
ImportiMIS_xmlPort.Import();

Download base 64 file blob field data to text file

procedure ExportDataAsText()
    var
      Instr: InStream;
    begin
        if not "File Data".HasValue then
            exit;

        CalcFields("File Data");
        "File Data".CreateInStream(Instr);
        DownloadFromStream(Instr, '', '', '', "File Name");
  end;

Call this web service function from postman

Method: Post

Authentication : Basic

Header: 

SOAPAction : "urn:microsoft-dynamics-schemas/codeunit/ImportSOFile:ImportSO"

Content Type: text/xml;charset=UTF-8

API: https://api.businesscentral.dynamics.com/v2.0/0083fe57-c7f0-4c02-9d2b-17d11377b1f1/Sandboxnew/WS/XXXXX/Codeunit/ImportSOFile

Body:

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <ImportSO xmlns="urn:microsoft-dynamics-schemas/codeunit/ImportSOFile">
            <fromMailID_iTxt>test@test.com</fromMailID_iTxt>
            <fileName>newtest01.csv</fileName>
            <fileData_BigTxt>SCwyNDcyNTgsMTEvMTEvMjAyMSwsLCw5MzQ2Mzc2MDk....CksMSw1My44MSwwLA==</fileData_BigTxt>
        </ImportSO>
    </Body>
</Envelope>















Postman Snapshot:








Thank you for reading 

Keep Sharing......Keep Growing......

No comments:

Post a Comment