1个回答
2015-07-17 · 知道合伙人软件行家
关注
展开全部
FileInfo fileInf = new FileInfo(@"path");
FileStream fs = fileInf.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
string ftpPath = @"你要上传的FTP路径ftp://ftp.domain.com/doesntexist.txt";
var request = (FtpWebRequest)WebRequest.Create(ftpPath);
request.Credentials = new NetworkCredential("user", "pass");
request.KeepAlive = true;
request.UseBinary = true;
request.Method = WebRequestMethods.Ftp.GetFileSize;
try
{
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
//文件存在,
request.Method = WebRequestMethods.Ftp.AppendFile;
Stream strm = request.GetRequestStream();
int buffLength = 2048;
byte[] buff = new byte[buffLength];
request.ContentLength = fileInf.Length;
int contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
}
catch (WebException ex)
{
FtpWebResponse response = (FtpWebResponse)ex.Response;
if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
{ //文件不存在
request.Method = WebRequestMethods.Ftp.UploadFile;
Stream strm = request.GetRequestStream();
int buffLength = 2048;
byte[] buff = new byte[buffLength];
request.ContentLength = fileInf.Length;
int contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
}
}
FileStream fs = fileInf.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
string ftpPath = @"你要上传的FTP路径ftp://ftp.domain.com/doesntexist.txt";
var request = (FtpWebRequest)WebRequest.Create(ftpPath);
request.Credentials = new NetworkCredential("user", "pass");
request.KeepAlive = true;
request.UseBinary = true;
request.Method = WebRequestMethods.Ftp.GetFileSize;
try
{
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
//文件存在,
request.Method = WebRequestMethods.Ftp.AppendFile;
Stream strm = request.GetRequestStream();
int buffLength = 2048;
byte[] buff = new byte[buffLength];
request.ContentLength = fileInf.Length;
int contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
}
catch (WebException ex)
{
FtpWebResponse response = (FtpWebResponse)ex.Response;
if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
{ //文件不存在
request.Method = WebRequestMethods.Ftp.UploadFile;
Stream strm = request.GetRequestStream();
int buffLength = 2048;
byte[] buff = new byte[buffLength];
request.ContentLength = fileInf.Length;
int contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
}
}
更多追问追答
追问
FileInfo fileInf = new FileInfo(@"path");
这个里面的path指什么?》
追答
本地路经如c:\a.txt
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询