-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFtpHelper.cs
175 lines (124 loc) · 6.53 KB
/
FtpHelper.cs
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using Utilities;
using System.Threading;
namespace FtpUploader
{
static class FtpHelper
{
private static FtpWebRequest request;
private static FtpWebResponse response;
private static Uri UriPath(string network)
{
return new Uri(string.Format("ftp://{0}", network));
}
/* 1st version of the method UploadFiles, used to upload one file. (KeepAlive = false) */
public static bool UploadFiles(string filePath, string networkPath, NetworkCredential credentials)
{
bool resultUpload = false;
System.Threading.Thread.Sleep(500);
Console.WriteLine("\nInitializing Network Connection..");
request = (FtpWebRequest)WebRequest.Create(UriPath(networkPath) + Path.GetFileName(filePath));
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UsePassive = true;
request.KeepAlive = false;
request.Credentials = credentials;
/* set ssl protection? */
//request.EnableSsl = true;
using (Stream requestStream = request.GetRequestStream())
{
System.Threading.Thread.Sleep(500);
Console.WriteLine("\nReading the file " + Path.GetFileName(filePath) + "...");
using (StreamReader sourceStream = new StreamReader(filePath))
{
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
}
}
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
System.Threading.Thread.Sleep(500);
Console.WriteLine("\nUpload of " + Path.GetFileName(filePath) + " file complete.");
Console.WriteLine("Request status: {0}", response.StatusDescription);
if (response.StatusDescription.Equals("226 Transfer complete.\r\n"))
resultUpload = true;
response.Close();
}
Console.WriteLine("\nClosing connection to {0}...", string.Format("ftp://{0}", networkPath));
return resultUpload;
}
/* 2nd version of the method UploadFiles, used to upload more than one file. You can notice the same request is opened until all the files are uploaded. (KeepAlive = true) */
public static bool[] UploadFiles(string[] filePath, string networkPath, NetworkCredential credentials)
{
bool[] resultUpload = new bool[filePath.Length];
resultUpload.Populate(false);
Console.WriteLine("\nInitializing Network Connection..");
System.Threading.Thread.Sleep(500);
for (int i = 0; i < filePath.Length; i++)
{
string currentFile = filePath[i].ToString();
request = (FtpWebRequest)WebRequest.Create(UriPath(networkPath) + Path.GetFileName(currentFile));
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UsePassive = true;
request.KeepAlive = true;
request.Credentials = credentials;
/* set ssl protection? */
//request.EnableSsl = true;
using (Stream requestStream = request.GetRequestStream())
{
System.Threading.Thread.Sleep(500);
byte[] fileContents;
using (FileStream fs = new FileStream(currentFile, FileMode.Open))
{
System.Threading.Thread.Sleep(500);
Console.WriteLine("\nReading the file " + Path.GetFileName(currentFile) + "...");
fileContents = new byte[fs.Length];
int numBytesRead = 0;
int numBytesToRead = (int)fs.Length;
while (numBytesToRead > 0)
{
int n = fs.Read(fileContents, numBytesRead, numBytesToRead);
if (n == 0) break;
numBytesRead += n;
numBytesToRead -= n;
}
numBytesToRead = fileContents.Length;
fs.Close();
request.ContentLength = fileContents.Length;
}
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
}
response = (FtpWebResponse)request.GetResponse();
System.Threading.Thread.Sleep(500);
Console.WriteLine("\nUpload of the file " + Path.GetFileName(filePath[i]) + " complete.");
Console.WriteLine("Request status: {0}", response.StatusDescription);
if (response.StatusDescription.Equals("226 Transfer complete.\r\n"))
resultUpload[i] = true;
}
Console.WriteLine("\nClosing connection to {0}...", string.Format("ftp://{0}", networkPath));
response.Close();
((IDisposable)response).Dispose();
/* before I used this implementation to close and dispose the response */
// using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
// {
// System.Threading.Thread.Sleep(500);
// Console.WriteLine("\nUpload of " + Path.GetFileName(filePath[i]) + " file complete.");
// Console.WriteLine("Request status: {0}", response.StatusDescription);
// if (response.StatusDescription.Equals("226 Transfer complete.\r\n"))
// resultUpload[i] = true;
// response.Close();
// }
// request = null;
//}
return resultUpload;
}
}
}