Tuesday, December 27, 2016

How to download FTP files using .NET Interop

download-1459071_1920_thumb

Recently i have worked on a project where the requirement was to upload and download the files from a FTP.  In this blog i will be explaining how to download the files using .NET Interop. There are several other blogs which have explained, how to download the files using ScriptingHost or .net interop but i have not found an example to download all the files from a particular folder.

These are couple of blogs i found, related to this.

http://www.archerpoint.com/blog/Posts/automating-command-line-functions-nav-rtc-transmit-ftp

http://www.dynamics.is/?p=583

Below is the code that downloads all the files from a particular FTP Folder to your local download folder. The GetFTPSetup is just another function to retrieve the setup values.

I have used two functions to download the files, the first function DownloadFilesFromFTP will find all the files and add to the list, then we will loop through the list to download the file using the second function.


PROCEDURE DownloadFilesFromFTP@1240060028();
     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;
       StreamReader@1240060010 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.StreamReader";
       List@1240060012 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Collections.Generic.List`1";
       i@1240060011 : Integer;
       PathHelper@1240060013 : DotNet "'mscorlib'.System.IO.Path";
       FileAttributes@1240060014 : DotNet "'mscorlib'.System.IO.FileAttributes";
       DotNetFile@1240060015 : DotNet "'mscorlib'.System.IO.File";
     BEGIN
       GetFTPSetup;
       FTPWebRequest := FTPWebRequest.Create(FTPSetup."FTP Download FilePath");
       FTPWebRequest.Credentials := NetworkCredential.NetworkCredential(FTPSetup."FTP UserName",FTPSetup."FTP Password");
       FTPWebRequest.UseBinary := TRUE;
       FTPWebRequest.UsePassive := TRUE;
       FTPWebRequest.KeepAlive := TRUE;
       FTPWebRequest.Method := 'NLST';
       FTPWebResponse := FTPWebRequest.GetResponse();
       StreamReader := StreamReader.StreamReader(FTPWebResponse.GetResponseStream());
       List := List.List();
       FileName := StreamReader.ReadLine();
       WHILE FileName <> '' DO BEGIN
         List.Add(FileName);
         FileName := StreamReader.ReadLine();
       END;
       FOR i:=1 TO List.Count DO BEGIN

        IF FORMAT(List.Item(i-1)) <> 'archive' THEN BEGIN
           DownloadFileFromFTP(FORMAT(List.Item(i-1)));
         END;
         
       END;
     END;

    [TryFunction]
     LOCAL PROCEDURE DownloadFileFromFTP@1240060029(FileToDownload@1240060010 : 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";
       TempBlob@1240060008 : TEMPORARY Record 99008535;
       FileName@1240060007 : Text;
       OutStream@1240060009 : OutStream;
     BEGIN
       GetFTPSetup;
       FTPWebRequest := FTPWebRequest.Create(FTPSetup."FTP Download FilePath" + FileToDownload);
       FTPWebRequest.Credentials := NetworkCredential.NetworkCredential(FTPSetup."FTP UserName",FTPSetup."FTP Password");
       FTPWebRequest.UseBinary := TRUE;
       FTPWebRequest.UsePassive := TRUE;
       FTPWebRequest.KeepAlive := TRUE;
       FTPWebRequest.Method := 'RETR';
       FTPWebResponse := FTPWebRequest.GetResponse();
       ResponseStream := FTPWebResponse.GetResponseStream();
       TempBlob.Blob.CREATEOUTSTREAM(OutStream);
       COPYSTREAM(OutStream,ResponseStream);
       FileName := FTPSetup."FTP Local Download FilePath" + FileToDownload; // 'download' + FORMAT(CURRENTDATETIME,0,'<Year4><Month,2><Day,2><Hours24><Minutes,2><Seconds,2>') +'.txt';
       TempBlob.Blob.EXPORT(FileName);
     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.


Share:

16 comments:

Reeta Devi said...

Great post, I like the info you share about Affordable ftp hosting

rose said...

I wish to indicate because of you only to bail me out of this specific trouble.As a consequence of checking through the net and meeting systems that were not beneficial, I thought my life was finished

Java Training in Bangalore|

Adam lee said...

It’s great to come across a blog every once in a while that isn’t the same out of date rehashed material. Fantastic read.
I’ve bookmarked your site, and I’m adding your RSS feeds to my Google account.
dot net training in bangalore

DAVIS MILLER said...

Your good knowledge and kindness in playing with all the pieces were
very useful. I don’t know what I would have done if I had not
encountered such a step like this.

Selenium Training in Bangalore

Careen joseph said...

Appreciating the persistence you put into your blog and detailed information you provide.
Besant technologies Marathahalli

Inigo joseph said...

Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts, have a nice weekend!
rprogramming training in bangalore

Inigo joseph said...

Thanks a lot very much for the high your blog post quality and results-oriented help. I won’t think twice to endorse to anybody who wants and needs support about this area.
rprogramming training in bangalore

mary Brown said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.
or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

srihariparu said...

Thanks for sharing this informative blog..
Electrical Project Center in Chennai | Electrical Project Center in Velachery

Joel Wong said...

Hello, I just wanted to leave a comment on how much I love your website looks! Great choice of font and color theme, and that background! By the way, if you ever need help to register a business, let me know, I know the best company secretarial services consultant, register a business immediately!

ASO Services said...

Thanks a lot for sharing this with all of us, I like it and we can communicate. Do you need buy app ratings and reviews. To boost app ranking and double app downloads now.

PLC Training Chennai said...

Good Post..Thanks for sharing



PLC Training in Chennai | PLC Training Institute in Chennai | PLC Training Center in Chennai | PLC SCADA Training in Chennai | PLC SCADA DCS Training in Chennai | Best PLC Training in Chennai | Best PLC Training Institute in Chennai | PLC Training Centre in Chennai | Automation Training in Chennai | Automation Training Institute in Chennai

VLSI Training Chennai said...

I read your blog its very attractive and impressive....

VLSI Training in Chennai | Best VLSI Training in Chennai | VLSI Training Centres in Chennai | VLSI Courses in Chennai | VLSI Training Courses in Chennai | VLSI Training Institute in Chennai | VLSI Training Institutes in Chennai | Best VLSI Training Institute in Chennai

Divya D said...

Informative blog... Thnq for sharing your information with us ...Continue more

Selenium training in Velachery | Selenium training in chennai

Unknown said...

mytectra placement Portal is a Web based portal brings Potentials Employers and myTectra Candidates on a common platform for placement assistance.

Naveen Yadav said...

How to delete a from FTP server.