tags:

views:

1234

answers:

4

I'm uploading multiple files using FtpWebRequest.
But for every file I'm opening and closing a connection.

How can I upload multiple files using the same connection?

Like a ftp client application, connect using username and password, change directory, upload file1, upload file2, upload file3, disconnect.

A: 

The default value of FtpWebRequest.KeepAlive is true. Are you explicitly setting KeepAlive to false?

MArag
+1  A: 

Hi,

FtpWebRequest is a bit unfortunate attempt to mimic HTTP behaviour but connect via FTP protocol. Problem is that HTTP is stateless protocol (connect - send one request - read one response - disconnect), but FTP is session-based protocol (connect - issue several commands - disconnect).

It would be better to use some of third party FTP components which can be found in the wild. Most of them are session-based.

Following code shows how to achieve it with our Rebex FTP/SSL

using Rebex.Net;

// create client and connect 
Ftp client = new Ftp();
client.Connect("ftp.example.org");

// authenticate 
client.Login("username", "password");

// change the current directory to '/top' 
client.ChangeDirectory("/top");

// upload the 'test.zip' file to the current directory at the server 
client.PutFile(@"c:\data\test.zip", "test.zip");

// upload the 'index.html' file to the specified directory at the server 
client.PutFile(@"c:\data\index.html", "/wwwroot/index.html");

// upload the content of 'c:\data' directory and all subdirectories 
// to the '/wwwroot' directory at the server 
client.PutFiles(@"c:\data\*", "/wwwroot", FtpBatchTransferOptions.Recursive);

// disconnect 
client.Disconnect();

Code is taken from www.rebex.net/ftp-ssl.net/tutorial-ftp.aspx

Martin Vobr
A: 

I had run into a similar issue. Transfer several related image files after corresponding DB updates are successful. I used the solution on this page 'http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx' to get a better performance. I too am opening and closing connection every time, except the images are loading in the background. I hope to find a better solution but this seems like a right direction.

JayD
A: 

Thank you.... I had the same problem too. This post helped me a lot.

Thanks,

Regards, Amith

Igor Stevebin