Geeks With Blogs
Fórmulas e Cenas Object Reference Not Set to an Instance of an Object

 FileInfo fileInf = new FileInfo("c:\myfile.txt");

FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://myFTPSite/myfile.txt"));

reqFTP.Credentials = new NetworkCredential("myUser","myPassword", "myFTPDomain");
 

reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;

reqFTP.ContentLength = fileInf.Length;

int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;

FileStream fs = fileInf.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();
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

 

Enjoy :)

Posted on Thursday, October 7, 2010 11:31 AM Geral , C# | Back to top


Comments on this post: How to: Upload a file using FTP

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Sglima | Powered by: GeeksWithBlogs.net