Using FTP over SSL/TLS (FTPS) in Business Central

I’ve made a .Net lib to access FTP over SSL/TLS functions from Nav/Business Central on Premise. It’s bases on limilabs Ftp.dll. This one is accepting all security certificate including self-signed. Do not confuse with SFTP that is FTP encapsulated in SSH.

Download it here : FTPS-For-Nav-2

Visual studio C# source project here FTPS-For-Nav-SOURCE

Install thoses two DLLs in service and roletailored client addin folders of Nav installation. Then you can use this function in C/AL. Exemple below, declare a variable DotNet of type “FTPS_For_Nav_DLL.FTPS”.

Connect function have optional arguments start from Range min (let them at 0 or blank string). RangeMin/Max are used for passive specific port range.

UseTLS : Yes use TLS protocol or No, use SSL.

If you have a setup FTPS asking for client certificate into the connect request you can provide the .cer client file path into “CertificateFile” argument. Please decode the .cer file manualy if encrypted to avoid any problem with the DLL encryption method and let CertificatePassword argument at blank string.

// Instanciate
DotNLFTPS := DotNLFTPS.FTPS();

// Login
DotNLFTPS.Connect(string url, string user, string password, int port, bool Explicit, bool UseTls, int RangeMin, int RangeMax, string CertificateFile, string CertificatePassword, string PrivateKeyFile);

// Download
TxtLError := DotNLFTPS.DownloadFile('ftpfolder\test.txt', 'D:\test.txt');
IF TxtLError <> '' THEN
  MESSAGE('Download failed with error: ' + TxtLError)
ELSE
  MESSAGE('Download sucess !');

// Upload
TxtLError := DotNLFTPS.UploadFile('D:\test.txt', 'ftpfolder\test2.txt');
IF TxtLError <> '' THEN
  MESSAGE('Upload failed with error: ' + TxtLError)
ELSE
  MESSAGE('Upload sucess !');

// Delete
DotNLFTPS.Deletefile('ftpfolder\test.txt');

// List root files
DotNLFTPS.ListFiles('ftpfolder');

// Loop through files
REPEAT
  TxtLFile := DotNLFTPS.GetNextFile();
  IF TxtLFile <> '' THEN
    TxtLFileList += TxtLFile + '\';
UNTIL TxtLFile = '';

// Close
DotNLFTPS.Disconnect();

MESSAGE(TxtLFileList); // show file list

9 Comments

  1. Hi,

    I am facing the below issue while running the code. Please provide some solution.

    Thank you.

    Microsoft Dynamics NAV
    —————————

    A call to FTPS_For_Nav_DLL.FTPS failed with this message: Request for the permission of type ‘System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’ failed.

    • Hello and sorry for the response time.
      I hope you resolve your problem on your own since elsewere it seem to be a file permission problem on location you provide to the upload or download function.
      You can try a Nav RENAME function to the same file location (after a save) to check if you got the same kind of error.

  2. Dear Maxime,

    I seem not to be able to connect to a FTPS.
    The connect function always returns false and if I try to do an upload I’ll get the following error:
    “Your are not currently connected to the server”

    This is my code:
    —————–
    DotNLFTPS := DotNLFTPS.FTPS();
    DotNLFTPS.Connect(FTPSetup.”FTP Server URL”,FTPSetup.User,FTPSetup.Password);
    Errortext := DotNLFTPS.UploadFile(FileName, FTPSetup.”Upload Folder” + File.Name);
    DotNLFTPS.Disconnect();
    IF Errortext ” THEN
    ERROR(Errortext);
    —————–
    the URL has the following format:
    ftps://ftp1.myserver.com:990/

    Is there something wrong with it?
    I tried it without the trailing backslash and also without the protocol in the beginning (ftp1.myserver.com:990). Didn’t change anything.
    It might be that I add a Port?

    Best regards
    Gerald

    • Hi Gerald,

      I’ve reuploaded the DLL at latest version so you have a port argument in the connect function. You can define the url without port and enter it in the appropriate argument.

      It’s used on several customers installation, hope this will work for you

  3. Hi Maxime,

    I’ve updated the DLL, restarted Server and Dev-Client, but get the following error on the constructor now:
    (Cannot load an instance of the following .NET Framework object)
    Eine Instanz des folgenden .NET Framework-Objekts kann nicht geladen werden: Montage FTPS-For-Nav-DLL, Version=1.20.0.0, Culture=neutral, PublicKeyToken=289b6370a7127bbd.

    As it has no parameters I’m not exactly sure what’s wrong:
    DotNLFTPS := DotNLFTPS.FTPS();
    It’s DotNet “Subtype” is:
    Prodware.FTPS.FTPS.’FTPS-For-Nav-DLL, Version=1.20.0.0, Culture=neutral, PublicKeyToken=289b6370a7127bbd’

    Best regards
    Gerald

  4. Hi Maxime,

    I’ve now downloaded the VS project and I’m able to build everything, but it also throws an error at the constructor when using the Test-Project (I also had to add one Connect()-parameter).

    The error thrown is:
    The file or assembly “Ftp, Version=2.0.19002.942, Culture=neutral, PublicKeyToken=6dc438ab78a525b3” or a reference of it was not found. The system cannot find the given file.

    I’m not exactly sure why, as the Limilabs FTP-DLL is at the Path given in the reference and has the correct version.
    Could you please check your project and resulting DLLs? Otherwise I’ll try to reimplement it.

    Thanks a lot!
    Gerald

    • Hi Gerald, I’m sorry I didn’t have time to work on this these days
      Otherwise you can have an alternative solution to install winscp on the server (free version) and execute winscp script.
      I’ve done this for old customer that can’t run dotnet.

      Here an winscp script exemple who download file localy then remove it remotly when it’s done:

      @echo off
      set TMPPATH=%temp%\csvtmp\
      mkdir “%TMPPATH%”
      winscp.com /command ^
      “open ftps://login:password@ftp1.host.net:21 -explicit” ^
      “get “”/folder/*”” -filemask=*.csv “”%TMPPATH%”” -delete” ^
      “exit”
      for /r “%TMPPATH%” %%f in (“*.*”) do move “%%f” “E:\EDI\”
      rmdir /s /q “%TMPPATH%”

      And this for upload and transfer local file into archive folder :

      @echo off
      set TMPPATH=%temp%\csvtmp\
      mkdir “%TMPPATH%”
      copy “E:\EDI\*.csv” %TMPPATH%”

      winscp.com /command ^
      “open ftps://login:password@ftp1.host.net:21 -explicit” ^
      “put “”E:\EDI\\*.csv”” “”/avisexp/”” -delete” ^
      “exit”
      for /r “%TMPPATH%” %%A in (“*.*”) do if not exist “E:\EDI\\%%~dpA” move “%%A” “E:\EDI\ARCHIVE”
      rmdir /s /q “%TMPPATH%”

  5. I know about the WinSCP-Variant (but I’m glad you offered it) and it is my fallback-solution if I can’t get this to work. It’s a bit hard to sell to the customer as it would require to reconfigure several work places.
    I’ll try to find what’s wrong in my copy of your project. If I can’t find it I’ll switch to WinSCP.

    Thanks a lot!

  6. Hi Gerald.
    I am trying to use this dlls in Al, but I can not manage them to work.
    Do they work in Al?

    In case of true, could you tell me how to define them in the dll.al file?

    Thank you ever so much

Leave a Reply

Your email address will not be published. Required fields are marked *