Piotr Jaszczyk
Piotr Jaszczyk

Reputation: 15

c# - uploading file on FTP is not working properly

I am using this code to upload file to FTP server:

String source = @"E:\\file\e1.txt";
String ftpurl = @"ftp://ftp.myftpserver.pl";
String ftpusername = @"[email protected]";
String ftppassword = @"mypassword";

string filename = Path.GetFileName(source);
string ftpfullpath = ftpurl + "/" + filename;
Debug.WriteLine("Uploading...");

var fs = File.OpenRead(source);

FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);
ftp.Timeout = -1;
ftp.KeepAlive = true;
ftp.UseBinary = true;
ftp.Method = WebRequestMethods.Ftp.UploadFile;

Stream ftpstream = ftp.GetRequestStream();
fs.CopyTo(ftpstream); 
ftpstream.Close();


Debug.WriteLine("Upload complete.");

File is quit big - about 50 mb. So, after uploading the file, appliaction hangs. Here is an output in debug window

Uploading... //immediately after executing the code
The thread 0xd50 has exited with code 0 (0x0). //after some time

And it never prints:

Upload complete.

Note, that after thread exits, upload is still in progress, it's not cancelled. I have noticed this problem, when I was trying to send multiple files from List in a loop - application hangs after uploading first file. Can someone help me?

//EDIT For testing purposes i Was trying to upload much smaller files in for loop. It seems working even after

The thread XXX has exited with code 0 (0x0). 

So it's because of size, but my server accepts much bigger files and I have free space on ftp. Any ideas?

Upvotes: 0

Views: 1259

Answers (1)

Richard Auger
Richard Auger

Reputation: 44

Most likely you have a timeout on the control channel. The data channel is busy and therefore happy...but any timeouts or keepalive you set in your code will unfortunately not apply here. An in-depth check of your FTP log should show the file uploading then the session closing unexpectedly. Check firewall and load balancer too if you have one (default values on an F5 did exactly the same thing to me...)

Upvotes: 1

Related Questions