etarvt
etarvt

Reputation: 73

c# Uploading files to ftp server

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.

Upvotes: 1

Views: 14085

Answers (2)

Haider Abbas
Haider Abbas

Reputation: 11

I used your code, had the same problem, and fixed it.

After you close the stream, you have to read reqFTP response by calling GetResponse() then close the response. Here is the code that fixes the problem:

// Original code
ftpStream.Flush();
ftpStream.Close();

// Here is the missing part that you have to add to fix the problem
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
this.lbl_error.Text = "Response:<br />" + response.StatusDescription;
response.Close();
reqFTP = null;
this.lbl_error.Visible = true;

You don't have to display the response, you can just get it and close it, I am displaying it just for reference.

Upvotes: 1

Daniel Dyson
Daniel Dyson

Reputation: 13230

start with wrapping your Stream creation in a using clause.

        using(Stream stream = new MemoryStream())
        {
            stream.Position = 0;

            // fill the stream

            bool res = this.UploadFile(stream, "test.csv", "dir");

        }

This will make sure that the stream is closed and that any unmanaged resources are disposed, whether an error occurs or not

Upvotes: 2

Related Questions