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

jensthomsen
Member Posts: 173
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?
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?
0
Comments
-
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 :?:0 -
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,
YukonMake Simple & Easy1 -
Hi Yukon
Thx for your answer! I think this will do the trick a long way down the roadThe 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....
0 -
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);
Steffan0 -
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 themServerFileName := 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);
0 -
Hi jensthomsen,
Check out this link http://stackoverflow.com/questions/6916805/why-base64-encoding-string-have-sign-in-the-last. It should explain why.0 -
To remove the padding A's you can replace this code:
//dnetByte := dnetMemoryStream.GetBuffer; dnetByte := dnetMemoryStream.ToArray();
0 -
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions