D365/AX7: Writing a file to a remote FTP/FTP Integration

Requirement

The requirement is writing a FTP file from Microsoft Dynamics 365 for Finance and Operations environment to a remote FTP.

Prerequisites

  1. Valid FTP Url
  2. Valid FTP username
  3. Valid FTP password
  4. FTP Url should be accessible from environment.

Sample Code

static void main(Args args)
{
TextStreamIo textStreamIO = TextStreamIo::constructForWrite();
System.Text.Encoding getUTF8;
System.IO.StreamReader streamReader;
System.Byte[] bytes;
System.Object request,response,credential;
System.Net.FtpWebRequest ftpRequest;
System.IO.Stream requestStream;
System.Net.FtpWebResponse ftpResponse;
Str1260 ftpFileName = "ftp://223.31.75.117" + @"/" + "filename.txt";
System.IO.StreamReader reader;
System.Char[] chars;
container con;
System.IO.Stream stream;
;

textStreamIO.writeExp(['Started']);
textStreamIO.writeExp(['This Is Test', ' For Barcode']);
textStreamIO.writeExp(['End']);

stream = textStreamIO.getStream();
stream.Position = 0;
reader = new System.IO.StreamReader(stream);
getUTF8 = System.Text.Encoding::get_UTF8();
bytes = getUTF8.GetBytes(reader.ReadToEnd());
request = System.Net.WebRequest::Create(new System.Uri(ftpFileName));
ftpRequest = request;
credential = new System.Net.NetworkCredential("username", "password");
ftpRequest.set_Credentials(credential);
ftpRequest.set_ContentLength(bytes.get_Length());
ftpRequest.set_Method("STOR");
ftpRequest.set_Proxy(null);
requestStream = ftpRequest.GetRequestStream();
requestStream.Write(bytes,0,bytes.get_Length());
requestStream.Close();
response = ftpRequest.GetResponse();
ftpResponse = response;
info('Processed.');
}


Leave a Reply