Wednesday, January 4, 2017

How to upload files to FTP server using .NET Interop

upload-1118929

In my previous posts i have explained how to download the files from the FTP Server or  move the files from one folder to another on the FTP server. Here are the posts if you have missed them

How to download files from FTP server using .NET Interop

How to move files from one folder to another on FTP server.

In this post i will show you the code i have used to upload the files to the FTP server.   The FTPSetup is a Setup table which I have used to store the path, ftp server, user, and password information.

In the first function (UploadFilesToFTP) I have used an Array to store the paths of all the files from the upload directory, then I iterate through the array and execute second function to upload the file. In the second function it uses FTP Web Request method ‘STOR’ to upload the file to the server.


LOCAL PROCEDURE UploadFilesToFTP@1240060030();
     VAR
       FTPWebRequest@1240060000 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.FtpWebRequest";
       FTPWebResponse@1240060001 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.FtpWebResponse";
       NetworkCredential@1240060002 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.NetworkCredential";
       WebRequestMethods@1240060003 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.WebRequestMethods+File";
       UTF8Encoding@1240060004 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.UTF8Encoding";
       ResponseStream@1240060005 : InStream;
       FileStream@1240060006 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.FileStream";
       TempBlob@1240060008 : TEMPORARY Record 99008535;
       FileName@1240060007 : Text;
       OutStream@1240060009 : OutStream;
       SearchOption@1240060013 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.SearchOption" RUNONCLIENT;
       ArrayHelper@1240060012 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Array" RUNONCLIENT;
       ArrayLength@1240060011 : Integer;
       DirectoryHelper@1240060014 : DotNet "'mscorlib'.System.IO.Directory" RUNONCLIENT;
       i@1240060015 : Integer;
       RelativeServerPath@1240060016 : Text;
       ClientFilePath@1240060017 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.String" RUNONCLIENT;
       PathHelper@1240060010 : DotNet "'mscorlib'.System.IO.Path";
     BEGIN
       GetFTPSetup;
       ArrayHelper := DirectoryHelper.GetFiles(FTPSetup."FTP Local Upload FilePath",'*.txt',SearchOption.TopDirectoryOnly);

      ArrayLength := ArrayHelper.GetLength(0);

      IF ArrayLength = 0 THEN
         EXIT;

      FOR i := 1 TO ArrayLength DO BEGIN
         RelativeServerPath := FORMAT(ArrayHelper.GetValue(i - 1));
          UploadFileToFTP(RelativeServerPath);
       END;
     END;

    LOCAL PROCEDURE UploadFileToFTP@1240060031(FileToUpload@1240060018 : Text);
     VAR
       FTPWebRequest@1240060000 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.FtpWebRequest";
       FTPWebResponse@1240060001 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.FtpWebResponse";
       NetworkCredential@1240060002 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.NetworkCredential";
       WebRequestMethods@1240060003 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.WebRequestMethods+File";
       UTF8Encoding@1240060004 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.UTF8Encoding";
       ResponseStream@1240060005 : InStream;
       FileStream@1240060006 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.FileStream";
      Stream@1240060020 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.Stream";
       FileDotNet@1240060019 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.File";
       TempBlob@1240060008 : TEMPORARY Record 99008535;
       FileName@1240060007 : Text;
       OutStream@1240060009 : OutStream;
       SearchOption@1240060013 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.SearchOption" RUNONCLIENT;
        i@1240060015 : Integer;
       RelativeServerPath@1240060016 : Text;
       ClientFilePath@1240060017 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.String" RUNONCLIENT;
       PathHelper@1240060010 : DotNet "'mscorlib'.System.IO.Path";
     BEGIN
       GetFTPSetup;
       FTPWebRequest := FTPWebRequest.Create(FTPSetup."FTP Local Upload FilePath" + PathHelper.GetFileName(FileToUpload));
       FTPWebRequest.Credentials := NetworkCredential.NetworkCredential(FTPSetup."FTP UserName",FTPSetup."FTP Password");
       FTPWebRequest.UseBinary := TRUE;
       FTPWebRequest.UsePassive := TRUE;
       FTPWebRequest.KeepAlive := TRUE;
       FTPWebRequest.Method := 'STOR';

      FileStream := FileDotNet.OpenRead(FileToUpload);
       Stream := FTPWebRequest.GetRequestStream();
       FileStream.CopyTo(Stream);
       Stream.Close;

      FTPWebResponse := FTPWebRequest.GetResponse();
       FTPWebResponse.Close();
     END;

Please leave your comments, feedback or any suggestions you have for me to improve my blog and also if you have any questions, feel free to post.

50 comments:

  1. FTP works great only if there is some expert to support it. On the other hand, Binfer can be run on any computer without separate server or client components. See Binfer as FTP alternative

    ReplyDelete
  2. Thanks for your informative blog!!! Your article helped me to understand the future of .net programming language. Keep on updating your with such awesome information.


    dot net training in chennai

    ReplyDelete
  3. Hi I read your post and tried to use it, but i'm facing some issues.

    The function i'm creating generates a csv-file on the base of an event from another record. The file i'm creation is a tempfile on the server.
    I can't seem to find the tempfile when uploading to ftp - I guess that is my problem.

    All this should work withot any user involved.

    I have determined by test the the function actually creates the file as i'm able to send the appropriate file attached to an email.

    How do I make the file available for transferring to remote ftp-server.
    Any help will be appriciated.

    Regards
    Mads Morre

    ReplyDelete
  4. Based on how you creating the file please check both on the server and client. What is the issues you are running into ? you are not able to find the file created ? or not able to upload that file to the ftp ?

    Thanks
    Suresh

    ReplyDelete
  5. Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
    dot-net training in chennai

    ReplyDelete
  6. Will this example works with NAV2013 (no R2)?

    ReplyDelete
  7. You have post an extremely valuable data. Furthermore, i have learned heaps of new data by utilizing your blog. Its truly supportive to me. Keep updating. I will take after these tips. Much obliged to you for sharing such point by point article.
    B.Com Project Center in Chennai | B.Com Project Center in Velachery

    ReplyDelete
  8. The best thing is that your blog really informative thanks for your great information!
    B.Com Project Center in Chennai | B.Com Project Center in Velachery

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. thanks for your great information. It's very ussefull

    ReplyDelete
  12. Much obliged to you a ton for furnishing people with an exceptionally dynamite probability to peruse basic audits from this site.
    Latest Updates

    ReplyDelete
  13. You have out done yourself this time.
    This is probably the best, most concise step by step guide.
    Top Institute for VLSI and Embedded in Hyderabad
    https://www.sumedhait.com/

    ReplyDelete
  14. Great post! This is very useful for me and gain more information, Thanks for sharing with us.

    easyblogging
    Article submission sites

    ReplyDelete
  15. Hi! Thank you for the share this information. This is very useful information for online blog review readers. Keep it up such a nice posting like this.
    oneplus mobile service center in chennai
    oneplus mobile service center
    oneplus mobile service centre in chennai

    ReplyDelete
  16. Hi,
    Best article, very useful and well explanation. Your post is extremely incredible.Good job & thank you very much for the new information, i learned something new. Very well written. It was sooo good to read and usefull to improve knowledge. Who want to learn this information most helpful. One who wanted to learn this technology IT employees will always suggest you take hadoop certification courses in bangalore.

    ReplyDelete
  17. Thank you for your post, What about if I don't know the File Name from the remote Server? I know the Path of the remote server BTW
    In Your example the function is expecting a text= FileToupload.
    The Line FileDotNet.OpenRead(FileToUpload) won't accept special Char like asterisk
    How I can handle this

    ReplyDelete
  18. I can's see the FTP server adress in your code. Where you tell the dotNet object the server or ip adress of the target?

    ReplyDelete
  19. The database is an essential thing to do every MNC'S so Microsoft azure introduces cloud computing to store data in large amounts. Learn Microsoft Azure from leading institutes and secure your future well.

    ReplyDelete
  20. I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries. keep it up.


    Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery


    ReplyDelete
  21. Great Post!!! Thanks for the data update and waiting for your new updates.
    what does .net framework do
    do i need .net framework

    ReplyDelete
  22. Thanks for sharing.This is very useful blog.We want more updates from you.we also provide dot net training and projectsplease visitbest final year project center in chennai.

    ReplyDelete
  23. If you need short-term access to a color laser printer, consulting with a printer rentals company is the best place to start. renting impresoras barcelona

    ReplyDelete
  24. Rekordbox DJ Crack + Serial Key (2022) Full Version. Rekordbox DJ Crack is a powerful piece of software that can play and edit any type of. RekordBox Download Crackeado

    ReplyDelete
  25. This production launched then nearly calls about researchers because of rapid research about 3D. Furthermore, this famous software is equipped because working of pragmatic tasks. Golden Software Surfer Crack

    ReplyDelete
  26. At Independence day of Pakistan, Pakistani's shouldn't forget the sacrifices that people they have given at the time of partition. Web Site

    ReplyDelete
  27. Such an excellent and interesting blog, do post like this more with more information, this was very useful,erp development company in Germany. Thanks for sharing. Keep updating your blog.

    ReplyDelete