Axaptapedia is now maintained by AgileCadence For more information please click here.
FtpWebRequest
From Axaptapedia
Since version 4 and in version 2009 you may use the .NET framework inside Dynamics AX. The FtpWebRequest class in the System.Net namespace handles FTP communication like upload, download, delete files and retrieve a list of files in a directory, etc.
Upload File[edit]
<xpp> static void uploadTestFile(Args _args) {
System.Object ftpo; System.Object ftpResponse; System.Net.FtpWebRequest request; System.IO.StreamReader reader; System.IO.Stream requestStream; System.Byte[] bytes; System.Net.NetworkCredential credential; System.String xmlContent; System.Text.Encoding utf8;
System.Net.FtpWebResponse response; ;
// Read file
reader = new System.IO.StreamReader("C:/test.xml");
utf8 = System.Text.Encoding::get_UTF8();
bytes = utf8.GetBytes( reader.ReadToEnd() );
reader.Close();
// little workaround to get around the casting in .NET
ftpo = System.Net.WebRequest::Create("ftp://ftp.company.com/dir/test.xml");
request = ftpo;
credential = new System.Net.NetworkCredential("user","password");
request.set_Credentials(credential);
request.set_ContentLength(bytes.get_Length());
request.set_Method("STOR");
// "Bypass" a HTTP Proxy (FTP transfer through a proxy causes an exception) // request.set_Proxy( System.Net.GlobalProxySelection::GetEmptyWebProxy() );
requestStream = request.GetRequestStream(); requestStream.Write(bytes,0,bytes.get_Length()); requestStream.Close(); ftpResponse = request.GetResponse(); response = ftpResponse; info(response.get_StatusDescription());
} </xpp>
Read from FTP[edit]
<xpp> static void readFromFTP(Args _args) {
System.Object ftpo;
System.Net.FtpWebRequest request;
System.IO.StreamReader reader;
System.Net.NetworkCredential credential;
System.Net.FtpWebResponse response;
System.String text;
;
ftpo = System.Net.WebRequest::Create("ftp://ftpserver.com/dir/myfile.txt");
request = ftpo;
credential = new System.Net.NetworkCredential("user","password");
request.set_Credentials(credential);
response = request.GetResponse(); reader = new System.IO.StreamReader(response.GetResponseStream()); text = reader.ReadToEnd(); info(text);
} </xpp>