How to download FTP files using .NET Interop
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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.
Leave a comment