Options

Nav 2013R2 - embedding files (PDF,txt etc) as binaries i xml

jensthomsenjensthomsen Member Posts: 173
edited 2014-05-28 in NAV Three Tier
Hi

In Nav2013R2 I want to embed files in XML as Base64 encoded binaries - we will confine to PDF-files. They should be embedded as Base64 encoded. I'm aware that I have to put the file into a stream, and then use the .Net Frameworks built-in support for Base64 converting with the System.Convert class (Convert.ToBase64String)...But how do I put all the things together?

Comments

  • Options
    jensthomsenjensthomsen Member Posts: 173
    I've found this 5 year old blog which seems to do the trick
    http://mibuso.com/blogs/mandyk/2009/06/16/net-com-wrapper-to-extract-navision-picture-into-xml-format/
    ...but in nav2009!
    In Nav2013R2 we can utilize the .Net interopt directly and have no need for creating a COM object.

    But still: How to do :?:
  • Options
    yukonyukon Member Posts: 361
    Hi jensthomsen,

    I have done picture to encode64 and zip to use for web service. I haven't test same as your requirement. Please take a look is it meet with your requirement...?
    Local Variable
    Name	        DataType	Subtype	Length
    dnetByte	DotNet	System.Array.'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
    dnetByte1	DotNet	System.Array.'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
    dnetConvert	DotNet	System.Convert.'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
    dnetMemoryStream	DotNet	System.IO.MemoryStream.'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
    dnetGZipStream	DotNet	System.IO.Compression.GZipStream.'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
    dnetCompressionMode	DotNet	System.IO.Compression.CompressionMode.'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
    objInstream	InStream		
    txtBase64Code	Text		
    
    CALCFIELDS(Picture);                                                      
    IF NOT Picture.HASVALUE THEN EXIT;
    dnetMemoryStream := dnetMemoryStream.MemoryStream;
    Picture.CREATEINSTREAM(objInstream);                              <--- You need to change base on your requirement.
    COPYSTREAM(dnetMemoryStream,objInstream);
    dnetByte := dnetMemoryStream.GetBuffer;
    
    dnetMemoryStream.Dispose;
    dnetMemoryStream := dnetMemoryStream.MemoryStream;
    
    dnetGZipStream := dnetGZipStream.GZipStream(dnetMemoryStream,dnetCompressionMode.Compress,TRUE);
    dnetGZipStream.Write(dnetByte,0,dnetByte.Length);
    dnetGZipStream.Close;
    dnetGZipStream.Dispose;
    dnetByte := dnetMemoryStream.ToArray;
    
    txtBase64Code := dnetConvert.ToBase64String(dnetByte);
    dnetMemoryStream.Dispose;
    CLEAR(objInstream);
    EXIT(txtBase64Code);
    

    Note : When you read back from your xml you need to unzip and decode it back.

    Best Regards,
    Yukon
    Make Simple & Easy
  • Options
    jensthomsenjensthomsen Member Posts: 173
    Hi Yukon

    Thx for your answer! I think this will do the trick a long way down the road :) The challenge for me lies in the fact that the file I want to embed is placed on a local drive or a network path. I could stream the file into a BLOB field and then just use you'r code, but it would be a more clean solution to stream directly from the file instead of streaming in and out of a BLOB field....
  • Options
    stecrostecro Member Posts: 3
    Hi,

    I had the same issue in NAV2013 R2. Converting PDF, Word or Excel files to base64 string and add to an xml-file for electronic invoices.
    Yukon, your code example did the trick for me! I removed the zip-part as I did not need it and just added a parameter 'tFile' entering the function and set txtBase64Code as Return Value:

    Thank you very much!
    IFile.OPEN(tFile);
    dnetMemoryStream := dnetMemoryStream.MemoryStream;
    IFile.CREATEINSTREAM(objInstream);
    COPYSTREAM(dnetMemoryStream,objInstream);
    
    dnetByte := dnetMemoryStream.GetBuffer;
    dnetMemoryStream.Dispose;
    dnetMemoryStream := dnetMemoryStream.MemoryStream;
    dnetMemoryStream.Write(dnetByte,0,dnetByte.Length);
    dnetMemoryStream.Close();
    dnetByte := dnetMemoryStream.ToArray;
    
    txtBase64Code := dnetConvert.ToBase64String(dnetByte);
    dnetMemoryStream.Dispose;
    CLEAR(objInstream);
    

    Steffan
  • Options
    jensthomsenjensthomsen Member Posts: 173
    Hi Stefan

    Your code helped me the last step - thanks! With the zip-part in Yukons code, my binary-file went wrong but now it looks right. The only thing left would be, that i'm getting a bunch of filler-'A's at the end of the file (like this: 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA')?? Is there any way to get rid of them
    ServerFileName := LocalFileManagementCU.UploadFileSilent(File_NameAndPathPAR); 
    
    LocalFile.OPEN(ServerFileName);
    LocalFile.CREATEINSTREAM(LocalInStream);
    
    LocalmemorystreamDN := LocalmemorystreamDN.MemoryStream;    //Constructor
    COPYSTREAM(LocalmemorystreamDN,LocalInStream);
    LocalArrayDN := LocalmemorystreamDN.GetBuffer;
    
    LocalmemorystreamDN.Dispose;
    LocalmemorystreamDN := LocalmemorystreamDN.MemoryStream;    //Constructor
    
    LocalmemorystreamDN.Write(LocalArrayDN,0,LocalArrayDN.Length);
    LocalmemorystreamDN.Close;
    LocalArrayDN := LocalmemorystreamDN.ToArray;
    
    LocalTxtBase64TXT := LocalConvertModeDN.ToBase64String(LocalArrayDN);
    LocalmemorystreamDN.Dispose;
    CLEAR(LocalInStream);
    
  • Options
    stecrostecro Member Posts: 3
    Hi jensthomsen,

    Check out this link http://stackoverflow.com/questions/6916805/why-base64-encoding-string-have-sign-in-the-last. It should explain why.
  • Options
    stecrostecro Member Posts: 3
    To remove the padding A's you can replace this code:
    //dnetByte := dnetMemoryStream.GetBuffer;
    dnetByte := dnetMemoryStream.ToArray();
    
  • Options
    nuno.silvanuno.silva Member Posts: 29
    yukon wrote: »

    Note : When you read back from your xml you need to unzip and decode it back.

    Best Regards,
    Yukon

    can you give the example of the decode?

    ===============
    Nuno Silva
Sign In or Register to comment.