首先感谢各位坛友 进入问题详情!
最近开发的Web程序 由于需求原因 现在需要把上传的文件放到指定的FTP服务器中!这是我第一次用程序和FTP打交道!所以不是很懂 希望各位坛友能帮帮我 提供类似的源码或者Demo!
谢谢各位了!小弟感激不尽!
------解决方案--------------------------------------------------------
ftpwebrequest
FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + FtpAddress + FtpRemotePath + fi.Name);
req.Credentials = new NetworkCredential(FtpUid, FtpPwd);
req.Method = WebRequestMethods.Ftp.UploadFile;
req.UseBinary = true;
req.ContentLength = length;
req.Timeout = 10 * 1000;
------解决方案--------------------------------------------------------
private static void UploadFile(string localFile)
{
FileInfo fi = new FileInfo(localFile);
FileStream fs = fi.OpenRead();
long length = fs.Length;
FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + FtpAddress + FtpRemotePath + fi.Name);
req.Credentials = new NetworkCredential(FtpUid, FtpPwd);
req.Method = WebRequestMethods.Ftp.UploadFile;
req.UseBinary = true;
req.ContentLength = length;
req.Timeout = 10 * 1000;
try
{
Stream stream = req.GetRequestStream();
int BufferLength = 2048;
byte[] b = new byte[BufferLength];
int i;
while ((i = fs.Read(b, 0, BufferLength)) > 0)
{
stream.Write(b, 0, i);
}
stream.Close();
stream.Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
------解决方案--------------------------------------------------------
LZ
看看这个。。看对你有帮助吗?
我被你说的有点晕
http://apps.hi.baidu.com/share/detail/14704249