0

While executing the below code I am getting the following error

(The underlying connection was closed The server committed a protocol violation )

. Please help on this. I almost go throw every article about the error still not getting the solution.

Thanks in Advance.

private static void Upload()
{
     string sourceFile = System.IO.Path.Combine(filepath, filename);
     string ftpServerIP = "Hostname";
     string ftpUserID = "UserName";
     string ftpPassword = "Password";

     FileInfo fileInf = new FileInfo(sourceFile);
     string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
     FtpWebRequest reqFTP;
     // Create FtpWebRequest object from the Uri provided
     reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));
     // Provide the WebPermission Credintials
     reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
     // By default KeepAlive is true, where the control connection is not closed
     // after a command is executed.
     reqFTP.KeepAlive = false;
     // Specify the command to be executed.
     reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
     // Specify the data transfer type.
     reqFTP.UseBinary = true;
     // Notify the server about the size of the uploaded file
     reqFTP.ContentLength = fileInf.Length;
     // The buffer size is set to 2kb
     int buffLength = 2048;
     byte[] buff = new byte[buffLength];
     int contentLen;
     // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
     FileStream fs = fileInf.OpenRead();
     //try
     //{
          // Stream to which the file to be upload is written
          Stream strm = reqFTP.GetRequestStream();
          // Read from the file stream 2kb at a time
          contentLen = fs.Read(buff, 0, buffLength);
          // Until Stream content ends
          while (contentLen != 0)
          {
              // Write Content from the file stream to the FTP Upload Stream
              strm.Write(buff, 0, contentLen);
              contentLen = fs.Read(buff, 0, buffLength);
           }
           // Close the file stream and the Request Stream
           strm.Close();
           fs.Close();
      //}
      //catch (Exception ex)
      //{
      //    Console.Write(ex.Message, "Upload Error");
      //}
 }
Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
Mufshid
  • 55
  • 2
  • 7
  • Show us [log file of `FtpWebRequest`](https://stackoverflow.com/q/9664650/850848); and a corresponding verbose log file of any FTP client (e.g. WinSCP) showing an upload of the same file. – Martin Prikryl Jun 13 '17 at 11:24
  • I would start by making sure you can do ftp. Either 1) Use cmd.exe and type > FTP. The test if upload can be made 2) Type in Windows Explorer ftp:\\172.x.x.x\. Then browse for item. This will verify if you have the credentials and the server allows ftp connections. – jdweng Jun 13 '17 at 11:35

1 Answers1

2

I found the answer for the problem. I was using SFTP server instead of FTP the code was entirely different.

So I changed the code.

    using Renci.SshNet;
    public static void UploadSFTPFile()
    {
        _SftpDetails = ReadIniFile(IniFilePath, "Sftp_Settings", SftpDetails);
        string sourcefile = System.IO.Path.Combine(filepath, filename);
        string destinationpath = _SftpDetails["SftpDestinationFolder"];
        string host = _SftpDetails["Host"];
        string username = _SftpDetails["UserName"];
        string password = _SftpDetails["Password"];
        int port = Convert.ToInt32(_SftpDetails["Port"]);  //Port 22 is defaulted for SFTP upload

        try
        {
            using (SftpClient client = new SftpClient(host, port, username, password))
            {
                client.Connect();
                client.ChangeDirectory(destinationpath);
                using (FileStream fs = new FileStream(sourcefile, FileMode.Open))
                {
                    client.BufferSize = 4 * 1024;
                    client.UploadFile(fs, Path.GetFileName(sourcefile));
                }
            }
        }
        catch(Exception ex)
        {
            Console.Write(ex.Message);
            return;
        }
    }

you have to install .sshNet from the NuGet Package Manager

Sal
  • 5,129
  • 5
  • 27
  • 53
Mufshid
  • 55
  • 2
  • 7