Creating a temporary file and upload it to a ftp (NAV 2016)

madsmorremadsmorre Member Posts: 40
Hi
I have issues on how to "attack" this task.
I have to create a csv-file (either by creating a file and write to it or by a xml-port). This file I should upload to a ftp-address. The task has to be without any involvement - is started by an NAV-event.

The task is in two parts then.
1. Create a file in developer environment (temp-file???) - i don't have to use it afterwards
2. Transfer the file to an ftp-server

Again - the task is without any involvement from any person.

I have read and tried a lot, but seems to get stock. I'm used to working in the 2-tier environment and are not yet familiar with the 3-tier.

Does anyone has suggestions to this?

Regards
/Mads Morre

Best Answer

Answers

  • Wisa123Wisa123 Member Posts: 308
    Check out Codeunit 419 to create Tempfiles on the server ( function ServerTempFileName, ...)

    Then let your XMLPort export into that file using a stream.

    For uploading the file onto the FTP there are several ways to do that.
    The best, in my opinion is to just use .NET to do that.

    Check out the FTPWebRequest class:
    https://msdn.microsoft.com/en-us//library/system.net.ftpwebrequest(v=vs.110).aspx

    Also there are plenty examples available online.
    Austrian NAV/BC Dev
  • madsmorremadsmorre Member Posts: 40
    HI

    I tried this:

    URL := parServer;
    IF parFolder <> '' THEN
    URL += parFolder+'/';
    FTPRequest := WebRequest.Create(URL);
    Credentials := Credentials.NetworkCredential(parUser,parPW);
    FTPRequest.Credentials := Credentials;
    FTPRequest.KeepAlive := FALSE;
    FTPRequest.UseBinary := TRUE;
    FTPRequest.Method := 'STOR';
    FTPRequest.UsePassive := TRUE;
    FileStream := FileA.OpenRead(parFileName);
    FTPRequest.GetRequestStream.Write(FileA.ReadAllBytes(parFileName), 0, FileStream.Length);

    Variables:
    WebRequest DotNet System.Net.WebRequest.'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    FTPRequest DotNet System.Net.FtpWebRequest.'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    Credentials DotNet System.Net.NetworkCredential.'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    FileStream DotNet System.IO.FileStream.'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    FileA DotNet System.IO.File.'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    URL Text 250

    But I get the following error - can you see why
    55eix1ijydss.png

  • ara3nara3n Member Posts: 9,256
    Here is another solution that works with an option source solution and works with SFTP as well.

    https://dynamicsuser.net/nav/b/ara3n/posts/moving-files-in-nav-with-ftp


    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • madsmorremadsmorre Member Posts: 40
    edited 2017-11-01
    Thank you all
    I have solved the issue with a little help.

    It is exactly as you wrote, Wisa123 - i needed the actual filename (to be) in the code as well.

    Thansk for your replies.
Sign In or Register to comment.