Send a Blob From Navision to a Javascript Add In

2019-09-16 18:06发布

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.

2条回答
【Aperson】
2楼-- · 2019-09-16 18:54

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.

查看更多
在下西门庆
3楼-- · 2019-09-16 18:59

I have this piece of code to add binary node to XML file. By this I was able to send file as binary data to web service. Try play with it maybe it will help. It uses ActiveX because it was written for Nav 5. You can replace it with .net.

  pNodeName@1210000 : Text[50];VAR pXMLDoc@1210001 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 6.0:{88D96A05-F192-11D4-A65F-0040963251E5}:'Microsoft XML, v6.0'.DOMDocument60";pPath@1210002 : Text[1024]);
  Att@1210009 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 6.0:{2933BF86-7B36-11D2-B20E-00C04F983E60}:'Microsoft XML, v6.0'.IXMLDOMElement";
  parent@1210008 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 6.0:{2933BF86-7B36-11D2-B20E-00C04F983E60}:'Microsoft XML, v6.0'.IXMLDOMElement";
  AdoStream@1210003 : Automation "{B691E011-1797-432E-907A-4D8C69339129} 6.1:{00000566-0000-0010-8000-00AA006D2EA4}:'Microsoft ActiveX Data Objects 6.1 Library'.Stream";

  Att := pXMLDoc.createElement(pNodeName);
  CREATE(AdoStream, TRUE);
  AdoStream.Type := 1; //Binary
  AdoStream.Open();
  AdoStream.LoadFromFile(pPath);
  Att.dataType('bin.hex');
  Att.nodeTypedValue := AdoStream.Read;
  AdoStream.Close();
  parent := pXMLDoc.documentElement;
  parent.appendChild(Att);
查看更多
登录 后发表回答