在AX4.0中使用C#脚本的实现
1,虽然ms收购了ax,但是在ax低版本(ver<=4.0)中,还没有办法直接使用ms现在主推的.net技术。
通常的做法是现在AX中天津.net的引用,然后才能在代码中使用.net的一些对象以实现我们需要的功能。
2,添加引用
3. job中代码范本。
//test download image from web //use this code can show web image on report print System.Net.HttpWebRequest httpRequest = null; System.Net.HttpWebResponse httpResponse = null; System.Net.CookieCollection cookies = null; CLRObject clro = null; System.IO.Stream stream; System.IO.StreamReader reader; str msg; BinData bidData=new BinData(); Image logoImage; Bitmap Bitmap; System.Drawing.Image webImage ; System.BitConverter bit; ; new InteropPermission(InteropKind::ClrInterop).assert(); clro = System.Net.WebRequest::Create("http://www5.sml.com/**Online/images/img1.JPG"); httpRequest = clro; httpResponse = httpRequest.GetResponse(); stream = httpResponse.GetResponseStream(); reader = new System.IO.StreamReader(stream); webImage = System.Drawing.Image::FromStream(stream);//webImage.FromStream( httpResponse.GetResponseStream(); webImage.Save("C:\\AXconfig\\001.jpg"); bidData.loadFile("C:\\AXconfig\\001.jpg"); Bitmap = bidData.getData();
4, 实现.NET的另外一种方法,示例代码,实现通过AX系统上载文件到非windows服务器。所有的C#对象都声明为ClrObject.
static boolean SML_UpLoadFileByStream(Str url,Str FullFileName,Container conHeader=connull(),Container conQueryString=connull()) { ClrObject PostArray; CLRObject BinaryReader; CLRObject WCHeaders,WCQueryString; ClrObject FileStream; ClrObject FileMode; ClrObject FileAccess; ClrObject PostStream; CLRObject WC; System.Exception clrException; boolean ret=false; int i; Container subcon; System.Net.WebClient xx; ; if (winapi::fileExists(FullFileName)) { try { FileStream = new CLRObject("System.IO.FileStream", FullFileName, CLRInterop::parseClrEnum("System.IO.FileMode","Open"), CLRInterop::parseClrEnum("System.IO.FileAccess", "Read")); BinaryReader = new CLRObject("System.IO.BinaryReader", FileStream); PostArray= BinaryReader.ReadBytes(System.Convert::ToInt32(FileStream.get_Length())); WC = new CLRObject("System.Net.WebClient"); if (conlen(conHeader)) { WCHeaders=WC.get_Headers(); for(i=1;i<=conlen(conHeader);i++) { subcon=conpeek(conHeader,i); WCHeaders.Add(conpeek(subcon,1),conpeek(subcon,2)); } } if (conlen(conQueryString)) { WCQueryString=WC.get_QueryString(); for(i=1;i<=conlen(conQueryString);i++) { subcon=conpeek(conQueryString,i); WCQueryString.Add(conpeek(subcon,1),conpeek(subcon,2)); } } PostStream=WC.OpenWrite(url, "POST"); PostStream.Write(PostArray, 0,PostArray.get_Length()); PostStream.close(); FileStream.close(); } catch(Exception::CLRError) { if (CLRInterop::isNull(PostStream) == false) { PostStream.Close(); } if (CLRInterop::isNull(FileStream) == false) { FileStream.Close(); } } ret=true; } return ret; }