一、批量文件上传。
//FtpURL:要上传到的FTP路径。
Uri uriAddress = new Uri(FtpURL);
//filename:上传前的文件名数组;filenames:上传后的文件名数组;UserName:FTP用户名; PassWord:FTP密码。
FileCopys(filename, uriAddress, filenames, UserName, PassWord);
#region 数据批量上传
protected void FileCopys(string[] fileName, Uri uploadUrl, string[] fileNameS, string username, string password)
{
try
{
for (int i = 0; i < fileNameS.Length; i++)
{
if (uploadUrl.Scheme == Uri.UriSchemeFtp)
{
FileInfo fileInf = new FileInfo(fileName[i]);
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uploadUrl + fileNameS[i]));
reqFTP.Credentials = new NetworkCredential(username, password);
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.KeepAlive = false;
reqFTP.UseBinary = false;
reqFTP.ContentLength = fileInf.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileInf.OpenRead();
Stream strm = reqFTP.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
int allbye = (int)fileInf.Length;
int startbye = 0;
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
startbye += contentLen;
}
strm.Close();
fs.Close();
}
}
}
catch
{
}
}
#endregion
二、单个文件上传
//FtpURL:要上传到的FTP路径。
Uri uriAddress = new Uri(FtpURL);
//filename:需要上传的文件名;filename:上传后的文件名;UserName:FTP用户名; PassWord:FTP密码。
FileCopy(filename, uriAddress, filename, UserName, PassWord);
protected void FileCopy(string fileName, Uri uploadUrl, string fileNameS, string username, string password)
{
//FileCopy(InitFile1, uriAddress, FileNames1, UserName, PassWord);
//FileCopy(InitFile5, uriAddress, FileNames5, UserName, PassWord);
//FileCopy(InitFile6, uriAddress, FileNames6, UserName, PassWord);
//FileCopy(InitFile7, uriAddress, FileNames7, UserName, PassWord);
//FileCopy(InitFile8, uriAddress, FileNames8, UserName, PassWord);
//FileCopy(InitFile9, uriAddress, FileNames9, UserName, PassWord);
//FileCopy(InitFile11, uriAddress, FileNames11, UserName, PassWord);
try
{
if (uploadUrl.Scheme == Uri.UriSchemeFtp)
{
FileInfo fileInf = new FileInfo(fileName);
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uploadUrl + fileNameS));
reqFTP.Credentials = new NetworkCredential(username, password);
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.KeepAlive = false;
reqFTP.UseBinary = false;
reqFTP.ContentLength = fileInf.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileInf.OpenRead();
Stream strm = reqFTP.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
int allbye = (int)fileInf.Length;
int startbye = 0;
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
startbye += contentLen;
}
strm.Close();
fs.Close();
}
}
catch
{
//MessageBox.Show("FTP站点用户名或密码错误,请与管理员联系!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。