Using DotNet to put a file on an FTP server

By using DotNet variables, you can achieve great things, which would otherwise take you a lot of effort / hassle.

Putting a file on an FTP server with C/AL would make you create batch files, specifically a file telling the FTP DOS command where to connect and what to do, without being really sure that the file you put there was actually put on the server…

 So bare with me, as I show you another DotNet piece of code I have up my sleeve.Off course we will need some variables:

All of the DotNet variables are by default run on the server. However, if you are using multitenancy or a cloud hosted NAV-installation, you might want to declare them client side.

Here is the actual code:

The code above will basically create a txt file on our FTP Server containing the SourceText. If you want to put an actual file, you could open the file and create a stream of it.

So, a quick run through:
– Create a connection string: this is basically the path to the file you want to write, it must start with the correct protocol identifier (ftp://)
– We set a few necessary parameters:
Method: STOR = Store a file on the server
KeepAlive: Not necessary to function, but doesn’t hurt
UseBinary: For file writing, it is better to transfer files binary instead of as text of course.
ContentLength: We need to specify the length of the content before even creating the Stream, this ensures that the object will create the correct headers to access the FTP server.
– GetRequestStream: Create a stream to our destination file in memory
– FTPResponse: we are able to get the response back from the FTP server, however these to lines of code are fully optional.

Tune in for my next post on how to get to the C/AL code out of .NET code.

5 thoughts on “Using DotNet to put a file on an FTP server

  1. HI, we are trying to implement your solution for Ftp file download but unable to find out .net variable like
    System.Net.FtpWebRequest, ,System.Net.FtpWebResponse etc.
    highly appreciate if you can help us to find how can we get these variable in Nav? id any exe needs to be install.?
    Many thanks in advance.

    • Hello Alok,

      In my code, the FTP Webrequest is the following:System.Net.FtpWebRequest.’System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′
      WebResponse is in the same namespace (System)

      Regards,
      Magno

  2. HI

    I want to put file on SFTP (SSH File Transfer Protocol) through Dynamics NAV 2013 r2.

    It will be great help from you.

    //Reagrds

    • Hi Charu,

      I did a quick search on the internet and it appears that to make connections to SFTP from .NET, you need to use libraries.
      If you download such a library, you’ll most probably be able to access it directly from NAV code as well.

      Regards,
      Joachim

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.