How to upload files to FTP server using .NET Interop
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.</p><p>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.
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
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.
Leave a comment