Microsoft Dynamics NAV: Upload with FTP using dotNET Interop

One of the biggest additions to the development environment with the advent of the RoleTailored Client Microsoft and Dynamics NAV Server was the availability of the Microsoft .NET Framework assemblies. This allows Microsoft Dynamics NAV objects can interact with .NET Framework objects. You can easily use assemblies from the .NET Framework class library, your own custom assemblies; or third-party assemblies. This allows you to extend your solution and not be limited to only the NAV or COM objects.

One such example of using dotNET Interoperability is the ability to use the dotNET framework to FTP files to a remote server. Without access to the dotNET framework it was necessary to create a third party COM control, call a batch file, or schedule some third party software to transfer a file. Now, you can generate and send files all from within NAV. This can centralize the task of generating and transmitting files to one application.

A function that can be used (and easily extended) to upload a file to a remote FTP server via dotNET Interop in NAV is listed below.

Local Variables

 Name			DataType	Subtype
FtpWebRequest		DotNet		System.Net.FtpWebRequest.'System'
FtpWebResponse		DotNet		System.Net.FtpWebResponse.'System'
NetworkCredential	DotNet		System.Net.NetworkCredential.'System'
Stream			DotNet		System.IO.Stream.'mscorlib'
SIOFile			DotNet		System.IO.File.'mscorlib'

UploadFile(siteaddress : Text[250];username : Text[80];password : Text[80];filename : Text[250];path : Text[250])

FtpWebRequest := FtpWebRequest.Create(siteaddress + '/' + filename);

FtpWebRequest.Credentials := NetworkCredential.NetworkCredential(username, password);
FtpWebRequest.Method := 'STOR';
FtpWebRequest.KeepAlive := TRUE;
FtpWebRequest.UseBinary := TRUE;
//FtpWebRequest.UsePassive := FALSE;
filename := path + filename;

FtpWebRequest.ContentLength := SIOFile.ReadAllBytes(filename).Length;
Stream := FtpWebRequest.GetRequestStream;
Stream.Write(SIOFile.ReadAllBytes(filename), 0, FtpWebRequest.ContentLength); Stream.Close; FtpWebResponse := FtpWebRequest.GetResponse; IF GUIALLOWED THEN BEGIN MESSAGE('%1 %2', FtpWebResponse.StatusCode, FtpWebResponse.StatusDescription); END;

Pingbacks and trackbacks (1)+

Add comment

Reading Stuff

Information in this document subject to change without notice.
All Software source code published is for demonstration and knowledge sharing purposes only. The Code is supplied "as is" without warranty as to result, performance or merchantability. Use at your own risk.
The opinions expressed herein are the opinions of the author and do not reflect those of any other entity.