Tuesday, January 12, 2010

Upload File From ftp in Asp.net

if (ContentUpload.HasFile)
{
string remoteFilename = ContentUpload.FileName;
string uri = "ftp Url Where file will be uploaded";
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Credentials = new NetworkCredential("ftpUserid", "ftpuserpass");
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = ContentUpload.PostedFile.ContentLength;
Stream OutputStream = reqFTP.GetRequestStream();
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen = (int)ContentUpload.PostedFile.InputStream.Length;

Stream InputStream = ContentUpload.PostedFile.InputStream;

contentLen = InputStream.Read(buff, 0, buffLength);

while (contentLen != 0)
{
OutputStream.Write(buff, 0, contentLen);

contentLen = InputStream.Read(buff, 0, buffLength);
}

OutputStream.Close();
InputStream.Close();

MessageLabel.Text = "File Uploaded.";
}

No comments: