Reading a file from FTP

Dear Experts,

May I know can I use the same codes to read (.xlsx) files in my local server, to read files from FTP ?

Same sets of codes, but off course the file path/directory will change from something like this: C:\MyDocs to ftp://ftp.myftpserver.com/MyDocs ? Thanks in advance!

Thanks in advance!

  • Hi cmchin,

     

    Yes you can use the came code, but

    make sure the relevant ports are open and you have the relevant permissions.

  • In reply to Sriyantha Roshan:

    Typically when having to import files from FTP, then I download the files to a local drive, before importing it into NAV.

    Sriyantha's suggestion works only if you have already an active connection to the FTP site (ie via Windows user permissions).

    You can find an example on how to download from FTP here: http://www.dynamics.is/?p=583 - example can also be used to HTTP downloads.

  • which nav version do you use?

    if vs. >= 2009 you could use ready to use .net classes to download a file from a ftp fileserver to a local temp directory and then with default mechanism into your nav system.

  • In reply to Erik P. Ernst:

    Hi Erik,

    1.When I am trying to use DownloadFtpFile method it is giving me this error-


    Microsoft Dynamics NAV
    ---------------------------

    A call to System.IO.File.ReadAllText failed with this message: Could not find file 'C:\Users\Vivek\AppData\Local\Temp\8\nrdata.txt'.
    ---------------------------
    OK
    ---------------------------

    2.What I have done I have created a file of the same name nrdata.txt in the above path,it executes without any error but i am not finding any file that is downloaded.Could you please help me out????
  • In reply to Vivek Rawat:

    Hi Vivek,
    My guess is that the file is located on the client pc.

    NAV doesn't read from the client, but from the server. So it's looking for the file in the above path relative to the NAV server.
  • In reply to Erik P. Ernst:

    Hi Erik,

    Thanks for the quick reply.

    Actually what I have done I have used function of the CodeUnit which I have downloaded from your link.

    Simply I have changed nothing in it and try to run that function -"DownloadFtpFile(Host : Code[20];UserID : Text[30];Password : Text[30];FileName : Text[30];ConvertCP : Boolean)" from the onrun trigger of a codeunit.
    passing the values in function.After that it is giving me error which i have mentioned in above reply.

    Host URl is the :ftp server of some other company and I am not downloading file from my local system.

    Can you please look into that where I am wrong??
  • In reply to Vivek Rawat:

    Well I created my own version of it, had a few more advanced requirements.

    But as I said before, the FileName ('C:\Users\Vivek\AppData\Local\Temp\8\nrdata.txt') needs to be a file in the C:\ of the NAV server.
    And if it's already on your NAV server, then the issue could be that the user running the NAV server does not have access to read the file.
    Remember that when you are running the function, then its not executed on the PC of your client, but on the actual NAV server. So if you have used the function to download a file, then it will also be stored on your server first. You have to "download" it to the client before you can store it on your own C:\.
  • In reply to Erik P. Ernst:

    Hi Erik,

    Thanks for the reply.

    I am running that CU from the server itself not from any Client.

     

    From the above screenshot you can see that user has successfully logged in but the file which i want to download from the FTP server "File is not found" showing in the command prompt.

     Please explain more on Permission-

     Should I create a same user on the ftp server and give the permission for that folder who is running NAV server??

    Thanks in Advance...

     

     

  • In reply to Vivek Rawat:

    Don't think that you understand me.

    We talk about different users.
    We have the user accessing the NAV client. And then we have the user account set to run the NAV server/service.

    In your case you also have the user account used to access the FTP server. I assume that you set that from where you call the function, so this user is not a problem here.

    The NAV client user is allowed to access files on their pc. The NAV service user needs to be allowed to access files on the server. So if C:\Users\Vivek\AppData\Local\Temp\8\nrdata.txt is a file on the NAV server, then the NAV service user needs to be allowed to read/write in that folder.
  • In reply to Erik P. Ernst:

    Just looked at it again.

    Here it actually looks to me like the problem is not what I thought it was. Nothing to do with permission yet. You didn't even get that far yet! Wink
    If you check the screen shot you attached. Here it says GET "D:\FTP\nrdata". That's not a valid name on an FTP server!
    What I always do, when testing FTP connections, is to use an FTP client manually, and then try to log in.
    I'm convinced that in this case, then "D:\FTP" is the actual physical location of the file on the FTP server - as seen from the server. So if you actually log into the FTP server, then the filename will be in the root of the ftp server. D:\FTP is just the data root on the server.

    So try having the FTP filename = "nrdata" - no folder and especially no D:\ - something the FTP protocol does not understand.
  • In reply to Erik P. Ernst:

    Hi Erik,

    Thanks so much for showing patience for answering my queries.

    That's working now and you got it finally my actual problem.
  • Hi All, I used this download and this works great for downloading a single, known file from an FTP site.
    However, we will not know the file name so I tried to use all sorts of wild cards - for example mget *.csv or mget *.* and nothing downloads. In reality I would like to download all files from the ftp site hence trying that command but nothing downloads.
    I wondered if this was something with the server, firewall but I thought I would just ask in case anyone has any tips?
    Thanks
Related