Tuesday, November 9, 2021

NAV 2016 - Save Image Blob File on Server - Convert from Base 64

 Hello Friends,

Today we received request from client to save the image to server folder from base 64 image string (it will call from powerapp using NAV web service)

Below function we create for development

---------------------------------------------------

    PROCEDURE SaveDocumentImage_gFnc@33027920(DocNo_iCod@33027921 : Code[20];Image_Base64_iBigTxt@33027931 : BigText) : Boolean;

    VAR

      PurchaseHeader_lRec@33027929 : Record 38;

      RecordLink_lRec@33027920 : Record 2000000068;

      FoundRecID_lRecID@33027922 : RecordID;

      FileSaveLocalFolderFullpath_lTxt@33027923 : Text;

      FileGloabalURLPath_lTxt@33027924 : Text;

      NameofFile_lTxt@33027925 : Text;

      Bytes@33027930 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Array";

      Convert@33027928 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Convert";

      MemoryStream@33027927 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.MemoryStream";

      OStream@33027926 : OutStream;

      TempBlob@33027932 : Record 99008535;

      IStream@33027933 : InStream;

      GLSetup_lRec@33027934 : Record 98;

      FileManagement_lCdu@33027935 : Codeunit 419;

    BEGIN

      IF DocNo_iCod = '' THEN

        ERROR('DCNo_iCod cannot be blank');


      IF FORMAT(Image_Base64_iBigTxt) = '' THEN

        ERROR('Image_Base64_iBigTxt cannot be blank');


      GLSetup_lRec.GET;

      GLSetup_lRec.TESTFIELD("Server Att Local Folder Path");

      GLSetup_lRec.TESTFIELD("Server Web Global URL for Att");


      NameofFile_lTxt := FORMAT(CURRENTDATETIME,0,'<Day,2>_<Month,2>_<Year4>_<Hours24>_<Minutes,2>_<Seconds,2>') +'_'+ FORMAT(RANDOM(100)) + '.jpg';

      FileSaveLocalFolderFullpath_lTxt := GLSetup_lRec."Server Att Local Folder Path" + NameofFile_lTxt;

      FileGloabalURLPath_lTxt := GLSetup_lRec."Server Web Global URL for Att" + NameofFile_lTxt;


      PurchaseHeader_lRec.GET(PurchaseHeader_lRec."Document Type"::Order,DocNo_iCod);

      FoundRecID_lRecID := PurchaseHeader_lRec.RECORDID;


      Bytes := Convert.FromBase64String(Image_Base64_iBigTxt);

      MemoryStream := MemoryStream.MemoryStream(Bytes);

      TempBlob.Blob.CREATEOUTSTREAM(OStream);

      TempBlob.Blob.CREATEINSTREAM(IStream);

      MemoryStream.WriteTo(OStream);


      FileManagement_lCdu.BLOBExportToServerFile(TempBlob,FileSaveLocalFolderFullpath_lTxt);


      //SaveImageAttachment_lFnc(Image_Base64_iBigTxt,GateEntryHeader_lRec);


      CLEAR(RecordLink_lRec);

      RecordLink_lRec.INIT;

      RecordLink_lRec."Record ID" := FoundRecID_lRecID;

      RecordLink_lRec.URL1 := FileGloabalURLPath_lTxt;

      RecordLink_lRec.Description := NameofFile_lTxt;

      RecordLink_lRec.Company := COMPANYNAME;

      RecordLink_lRec."User ID" := USERID;

      RecordLink_lRec.Created := CURRENTDATETIME;

      RecordLink_lRec.Type := RecordLink_lRec.Type::Link;

      RecordLink_lRec.INSERT;

      COMMIT;


      EXIT(TRUE);

    END;


==============






No comments:

Post a Comment