I have a Blob Object in NAV, which represents an Excel document. I want this document to be displayed in my JavaScript Control AddIn (run in Web- and Windows-Client). But I am not sure how to handle this problem. Later in the AddIn, I need the Blob as a binary string.
So far I tried two things.
First try
I build an interface with this method
[ApplicationVisible]
void SetExcelDocument(ExcelDocument ExcelDocument);
and the ExcelDocument
- Object looks like this
[Serializable]
public class ExcelDocument
{
//Constructor
public ExcelDocument()
{
CurrentExcelDocument = null;
}
//store Blob
public Stream CurrentExcelDocument { get; set; }
}
So in NAV, i convert the Blob in a .Net MemoryStream variable, which looks like this
MemStream DotNet System.IO.System.IO.MemoryStream.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
and the code like this
TechnicalSpreadsheet.CALCFIELDS(Document); //Get Blob (Document is table column name)
TechnicalSpreadsheet.Document.CREATEINSTREAM(Lstr_BlobInstream);
MemStream := MemStream.MemoryStream();
COPYSTREAM(MemStream,Lstr_BlobInstream);
ExcelDocument := ExcelDocument.ExcelDocument; //Initialize Object
ExcelDocument.CurrentExcelDocument := MemStream; //set MemoryStream
CurrPage.spreadsheet.SetExcelDocument(ExcelDocument); //Call function in AddIn
The code is executed, when the AddIn is ready. the Nav variable ExcelDocument
is defined as this
ExcelDocument DotNet <Reference to Object here>, Version=8.0.0.0, Culture=neutral, PublicKeyToken=d02dbb9bbac93844'
So the problem here is, I can only set an instance of ExcelDocument
in NAV, when the property "RunOnClient" is set to yes. But this is not working for the WebClient (Source). But however even setting the property to yes, won`t send the data to the AddIn.
Second Try
For the second try, I changed the datatype. So I used a byte[]
instead of a stream. So ich changed the interface to this
[ApplicationVisible]
void SetExcelDocument(byte[] ExcelDocument);
and just changed the AddIn function call in NAV to this
CurrPage.spreadsheet.SetExcelDocument(MemStream.ToArray); //Call function in AddIn
and removed the ExcelDocument
variable.
This works but the data does not look like binary data. I would expect some unreadable stuff when displaying it in the web console, but I get readable letters and numbers instead. And I am not sure how to convert this to a binary string.
There is some nice input from Vjeko, but this does not work for me either.
I found a solution to send the blob to JavaScript Control AddIn. Here is what i did.
In the interface of the control AddIn I defined the method
[ApplicationVisible]
void SetExcelDocument(ExcelDocument exceldoc);
where the ExcelDocument
- Object looks like this
[Serializable]
public class ExcelDocument
{
public string ExcelJson; //Excel Content stored in JSON format
public byte[] Excel
{
get
{
return null; //Not needed in the AddIn
}
set
{
IConverter converter = new Converter();
ExcelJson = converter.ArrayBufferOfExcelDocToJson(value);
}
}
}
The magic is now happening in the new Converter
- class. Shortly I convert the byte array to a Stream, then read the Stream (respectively the Excel document) with the help of EPPlus and finally convert the content of the document with Newtonsoft.JSON to a Json string (variable ExcelJson
).
In NAV it will look like this.
//Get Blob (Document is table column name)
TechnicalSpreadsheet.Document.CREATEINSTREAM(Lstr_BlobInstream);
//init Memory Stream
MemStream := MemStream.MemoryStream();
COPYSTREAM(MemStream,Lstr_BlobInstream);
//Init DotNet Variable
ExcelDocument := ExcelDocument.ExcelDocument;
//Set byte[]
ExcelDocument.Excel := MemStream.GetBuffer;
//Call function in AddIn
CurrPage.spreadsheet.SetExcelDocument(ExcelDocument);
This works for me.