Páginas

12/11/2010

[C#] Upload de um arquivo para um servidor FTP

private void ftpUpload( string pathFile, string FTPHost, string FTPUser, string FTPPass )
{

FileInfo fileInfo
= new FileInfo(pathFile);
FtpWebRequest ftpWebRequest
= (FtpWebRequest)WebRequest.Create(FTPHost + Path.GetFileName(pathFile));

ftpWebRequest.Credentials
= new NetworkCredential(FTPUser, FTPPass);
ftpWebRequest.Method
= WebRequestMethods.Ftp.UploadFile;
ftpWebRequest.UsePassive
= true; //request.UseBinary = true; ftpWebRequest.UseBinary = false; // em campos texto evita linhas em branco ftpWebRequest.KeepAlive = false;

ftpWebRequest.ContentLength
= fileInfo.Length;
int buffLength = 2048; byte[] buff = new byte[buffLength];

Stream ftpStream
= ftpWebRequest.GetRequestStream();

FileStream fileStream
= fileInfo.OpenRead();
int fileLen = fileStream.Read(buff, 0, buffLength);
while (fileLen != 0)
{
ftpStream.Write(buff,
0, fileLen);
fileLen
= fileStream.Read(buff, 0, buffLength);
}

fileStream.Close();
ftpStream.Close();

}

Nenhum comentário: