一、文件的上传参考
思想,C#FTP上传
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
|
/// <summary>
/// 上传
/// </summary>
/// <param name="filename">要上传的本地文件名</param>
public
void
Upload(
string
filename)
{
FileInfo fileInf =
new
FileInfo(filename);
string
uri = ftpURI + fileInf.Name;
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(
new
Uri(uri));
reqFTP.Credentials =
new
NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive =
false
;
reqFTP.Proxy =
null
;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary =
true
;
reqFTP.ContentLength = fileInf.Length;
int
buffLength = 2048;
byte
[] buff =
new
byte
[buffLength];
int
contentLen;
FileStream fs = fileInf.OpenRead();
try
{
Stream strm = reqFTP.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while
(contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
}
catch
(Exception ex)
{
Error_Log(
"FTP上传文件时发成错误,详细错误参数请查看错误日志。"
,
"Upload Error --> "
+ ex.Message +
"\r\n"
+ ex.StackTrace);
}
}
|