日期:2014-05-18  浏览次数:20912 次

C# ftp问题,求高手。急事
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create("D:\\C.txt");
报错。
无法将类型为“System.Net.FileWebRequest”的对象强制转换为类型“System.Net.FtpWebRequest”。
创建的明明是WebRequest,怎么报错是这个了?求ftp登录,下载,创建。

------解决方案--------------------
public bool Upload(string filename, string newfilename)
{
bool result = false;
try
{
this.bError = false;
string ftpURI = @"ftp://xxxx.com/imgcode/";
string ftpname = "ftpusername";
string ftppwd = "ftppassword";

FileInfo fileInf = new FileInfo(filename);
FtpWebRequest reqFTP;
newfilename = string.IsNullOrEmpty(newfilename) ? fileInf.Name : newfilename;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + newfilename));
reqFTP.Credentials = new NetworkCredential(ftpname, ftppwd);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.EnableSsl = false;
reqFTP.UseBinary = true;
reqFTP.UsePassive = true;
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);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
reqFTP.Abort();
result = true;
}
catch (Exception ex)
{
this.cErrorMsg = ex.Message;
this.bError = true;
}
return result;
}