Hi there,
I have a problem with uploading files to ftp server. I have a few buttons. Every button uploads different files to the ftp. The first time when a button is clicked the file is uploaded successfully, but the second and later tries fail. It gives me "The operation has timed out". When I close the site and then open it again I can upload again only one file. I am sure that I can override files on the ftp. Here's the code:
protected void btn_export_OnClick(object sender, EventArgs e)
{
Stream stream = new MemoryStream();
stream.Position = 0;
// fill the stream
bool res = this.UploadFile(stream, "test.csv", "dir");
stream.Close();
}
private bool UploadFile(Stream stream, string filename, string ftp_dir)
{
stream.Seek(0, SeekOrigin.Begin);
string uri = String.Format("ftp://{0}/{1}/{2}", "host", ftp_dir, filename);
try
{
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Credentials = new NetworkCredential("user", "pass");
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.KeepAlive = false;
reqFTP.UseBinary = true;
reqFTP.UsePassive = true;
reqFTP.ContentLength = stream.Length;
reqFTP.EnableSsl = true; // it's FTPES type of ftp
int buffLen = 2048;
byte[] buff = new byte[buffLen];
int contentLen;
try
{
Stream ftpStream = reqFTP.GetRequestStream();
contentLen = stream.Read(buff, 0, buffLen);
while (contentLen != 0)
{
ftpStream.Write(buff, 0, contentLen);
contentLen = stream.Read(buff, 0, buffLen);
}
ftpStream.Flush();
ftpStream.Close();
}
catch (Exception exc)
{
this.lbl_error.Text = "Error:<br />" + exc.Message;
this.lbl_error.Visible = true;
return false;
}
}
catch (Exception exc)
{
this.lbl_error.Text = "Error:<br />" + exc.Message;
this.lbl_error.Visible = true;
return false;
}
return true;
}
Does anyone have idea what may cause this strange behaviour? I think I'm closing all streams accurately. Could this be related with the ftp server settings? The admin said that the ftp handshake never happened second time.