C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Uploading files to an FTP server


Posted Date: 24 Jun 2008    Resource Type: Code Snippets    Category: FTP
Author: GiribabuMember Level: Gold    
Rating: Points: 10



Below is the code which will upload files from server/local pc to FTP server, for full code refer to my previous post. link is given above.

public static void UploadFile(string fileName)
{
// Get the object used to communicate with the server.
FtpWebRequest reqObj = (FtpWebRequest) FtpWebRequest.Create(getServerUri() + fileName);

// Set the credentials
// This application reads the UserName & Password from settings file to logon to FTP site.
reqObj.Credentials = new NetworkCredential(FTPSettings.UserName, FTPSettings.Password);

// Turn off KeepAlive (will close connection on completion)
reqObj.KeepAlive = false;

// Specifying the data transfer type [binary].
reqObj.UseBinary = true;

// Define the action required (in this case, uploading a file).
reqObj.Method = WebRequestMethods.Ftp.UploadFile;

// The buffer size is set to 2kb
//int buffLength = 2048;
byte[] buffer = new byte[2048];
int contentLen;

// Opens a file stream (System.IO.FileStream) to read the file
// to be uploaded
FileStream fs = File.OpenRead(fileName);

try
{
// Stream to which the file to be upload is written
using( Stream requestStream = reqObj.GetRequestStream() )
{
try
{
// Read from the file stream 2kb at a time
contentLen = fs.Read(buffer, 0, buffer.Length);

// Till Stream content ends
while( contentLen != 0 )
{
// Write Content from the file stream to the FTP Upload
// Stream
requestStream.Write(buffer, 0, contentLen);
contentLen = fs.Read(buffer, 0, buffer.Length);
}
}
catch( Exception ex )
{
ErrorLog.LogError(ex);
}
// Close the file stream and the Request Stream
requestStream.Close();
}
fs.Close();
}
catch( Exception ex )
{
ErrorLog.LogError(ex.Message.ToString());
}
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Simplest Way of Sending Email
Previous Resource: Upload files using FTP with .NET
Return to Discussion Resource Index
Post New Resource
Category: FTP


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use