FtpWebRequest class to upload a file to ftp server (destination )
This Function shows how to upload a file ftp server. this function simply return true or false
if uploaded successfully it will return true else false
public bool Upload(FileInfo fileInfo)
{
string uri = "";
string serverIp = "Studentacad.com";
string Username = "Aamir";
string Password = "Hasan";
uri = "ftp://" + serverIp + "/" + fileInfo.Name;
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Credentials = new NetworkCredential(Username, Password);
reqFTP.KeepAlive = false;
reqFTP.Proxy = null;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.Timeout = 8000;
reqFTP.ContentLength = fileInfo.Length;
int buffLength = 433664;//1048;// *16;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileInfo.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();
return true;
}
catch (Exception ex)
{
if (reqFTP != null)
{
reqFTP.Abort();
}
return false
}
}
http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx
http://www.asp.net/learn/videos/video-448.aspx
Revision number 3, Tuesday, January 19, 2010 2:58:45 PM by tmorton
You must Login to comment.
|
Sat, Jan 2, 2010 12:18 AM
by amanbhullar
|
Hi, please explain your code too...
|
|
Mon, Jan 18, 2010 9:16 PM
by pnv.ravikiran
|
good one Aamir.. keep the good work up.
|
Revision #5
Sun, Aug 23, 2009 2:59 PM
by
|
FtpWebRequest
FtpWebRequest The FtpWebRequest class enables you to programatically create FTP connections to FTP Servers and transfer files. If you are interested in using the FtpWebRequest class to upload files to a server,here is a code sample: FtpWebRequest ftpRequest;
|