|
|
@@ -1,5 +1,6 @@
|
|
1
|
1
|
using System;
|
|
2
|
2
|
using System.IO;
|
|
|
3
|
+using System.Net;
|
|
3
|
4
|
using System.Web;
|
|
4
|
5
|
using System.Web.UI.WebControls;
|
|
5
|
6
|
|
|
|
@@ -137,5 +138,114 @@ namespace CallCenter.Utility
|
|
137
|
138
|
fileStream.Close();
|
|
138
|
139
|
}
|
|
139
|
140
|
}
|
|
|
141
|
+
|
|
|
142
|
+
|
|
|
143
|
+ /// <summary>
|
|
|
144
|
+ /// FTP上传文件
|
|
|
145
|
+ /// </summary>
|
|
|
146
|
+ /// <param name="fileinfo">需要上传的文件</param>
|
|
|
147
|
+ /// <param name="targetDir">目标路径</param>
|
|
|
148
|
+ /// <param name="hostname">ftp地址</param>
|
|
|
149
|
+ /// <param name="username">ftp用户名</param>
|
|
|
150
|
+ /// <param name="password">ftp密码</param>
|
|
|
151
|
+ public static string UploadFile(FileInfo fileinfo, string targetDir, string hostname, string username, string password)
|
|
|
152
|
+ {
|
|
|
153
|
+ //1. check target
|
|
|
154
|
+ string target;
|
|
|
155
|
+ if (targetDir.Trim() == "")
|
|
|
156
|
+ {
|
|
|
157
|
+ return "";
|
|
|
158
|
+ }
|
|
|
159
|
+ target = Guid.NewGuid().ToString(); //使用临时文件名
|
|
|
160
|
+
|
|
|
161
|
+
|
|
|
162
|
+ string URI = "FTP://" + hostname + "/" + targetDir + "/" + target;
|
|
|
163
|
+ ///WebClient webcl = new WebClient();
|
|
|
164
|
+ System.Net.FtpWebRequest ftp = GetRequest(URI, username, password);
|
|
|
165
|
+
|
|
|
166
|
+ //设置FTP命令 设置所要执行的FTP命令,
|
|
|
167
|
+ //ftp.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails;//假设此处为显示指定路径下的文件列表
|
|
|
168
|
+ ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
|
|
|
169
|
+ //指定文件传输的数据类型
|
|
|
170
|
+ ftp.UseBinary = true;
|
|
|
171
|
+ ftp.UsePassive = true;
|
|
|
172
|
+
|
|
|
173
|
+ //告诉ftp文件大小
|
|
|
174
|
+ ftp.ContentLength = fileinfo.Length;
|
|
|
175
|
+ //缓冲大小设置为2KB
|
|
|
176
|
+ const int BufferSize = 204800;
|
|
|
177
|
+ byte[] content = new byte[BufferSize - 1 + 1];
|
|
|
178
|
+ int dataRead;
|
|
|
179
|
+
|
|
|
180
|
+ //打开一个文件流 (System.IO.FileStream) 去读上传的文件
|
|
|
181
|
+ using (FileStream fs = fileinfo.OpenRead())
|
|
|
182
|
+ {
|
|
|
183
|
+ try
|
|
|
184
|
+ {
|
|
|
185
|
+ //把上传的文件写入流
|
|
|
186
|
+ using (Stream rs = ftp.GetRequestStream())
|
|
|
187
|
+ {
|
|
|
188
|
+ do
|
|
|
189
|
+ {
|
|
|
190
|
+ //每次读文件流的2KB
|
|
|
191
|
+ dataRead = fs.Read(content, 0, BufferSize);
|
|
|
192
|
+ rs.Write(content, 0, dataRead);
|
|
|
193
|
+ } while (!(dataRead < BufferSize));
|
|
|
194
|
+ rs.Close();
|
|
|
195
|
+ }
|
|
|
196
|
+
|
|
|
197
|
+ }
|
|
|
198
|
+ catch (Exception ex) { }
|
|
|
199
|
+ finally
|
|
|
200
|
+ {
|
|
|
201
|
+ fs.Close();
|
|
|
202
|
+ }
|
|
|
203
|
+
|
|
|
204
|
+ }
|
|
|
205
|
+
|
|
|
206
|
+ ftp = null;
|
|
|
207
|
+ //设置FTP命令
|
|
|
208
|
+ ftp = GetRequest(URI, username, password);
|
|
|
209
|
+ ftp.Method = System.Net.WebRequestMethods.Ftp.Rename; //改名
|
|
|
210
|
+ ftp.RenameTo = fileinfo.Name;
|
|
|
211
|
+ try
|
|
|
212
|
+ {
|
|
|
213
|
+ ftp.GetResponse();
|
|
|
214
|
+ return fileinfo.Name;
|
|
|
215
|
+ }
|
|
|
216
|
+ catch (Exception ex)
|
|
|
217
|
+ {
|
|
|
218
|
+ ftp = GetRequest(URI, username, password);
|
|
|
219
|
+ ftp.Method = System.Net.WebRequestMethods.Ftp.DeleteFile; //删除
|
|
|
220
|
+ ftp.GetResponse();
|
|
|
221
|
+ throw ex;
|
|
|
222
|
+ }
|
|
|
223
|
+ finally
|
|
|
224
|
+ {
|
|
|
225
|
+ //fileinfo.Delete();
|
|
|
226
|
+ }
|
|
|
227
|
+
|
|
|
228
|
+ // 可以记录一个日志 "上传" + fileinfo.FullName + "上传到" + "FTP://" + hostname + "/" + targetDir + "/" + fileinfo.Name + "成功." );
|
|
|
229
|
+ ftp = null;
|
|
|
230
|
+
|
|
|
231
|
+ #region
|
|
|
232
|
+ /*****
|
|
|
233
|
+ *FtpWebResponse
|
|
|
234
|
+ * ****/
|
|
|
235
|
+ //FtpWebResponse ftpWebResponse = (FtpWebResponse)ftp.GetResponse();
|
|
|
236
|
+ #endregion
|
|
|
237
|
+ }
|
|
|
238
|
+
|
|
|
239
|
+
|
|
|
240
|
+ private static FtpWebRequest GetRequest(string URI, string username, string password)
|
|
|
241
|
+ {
|
|
|
242
|
+ //根据服务器信息FtpWebRequest创建类的对象
|
|
|
243
|
+ FtpWebRequest result = (FtpWebRequest)FtpWebRequest.Create(URI);
|
|
|
244
|
+ //提供身份验证信息
|
|
|
245
|
+ result.Credentials = new System.Net.NetworkCredential(username, password);
|
|
|
246
|
+ //设置请求完成之后是否保持到FTP服务器的控制连接,默认值为true
|
|
|
247
|
+ result.KeepAlive = false;
|
|
|
248
|
+ return result;
|
|
|
249
|
+ }
|
|
140
|
250
|
}
|
|
141
|
251
|
}
|