Stream writes only 512 bytes to a file on FTP server using C#

317 views Asked by At

I'm trying to copy a file from my local computer to an FTP server using C#. When I use the code below, the file is fully copied to the FTP server but **the original lines are cut into pieces of only 512 bytes long while they should be 1152,1126 or 1024 bytes long. ** The example file I used has now 16 lines instead of 7.

    public void uploadLOTFILE(string username, string password)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://mysite.mine/mypathandfilename");
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Credentials = new NetworkCredential(username, password);
        request.KeepAlive = true;
        System.IO.Stream rs = request.GetRequestStream();

        var lines = File.ReadLines(@"myLocalFile.txt");
        foreach (var line in lines)
        {
            byte[] buffer = Encoding.ASCII.GetBytes(line);                
            Console.WriteLine("buffer.length:" + buffer.Length.ToString());
            rs.Write(buffer,0,buffer.Length);
        }
        rs.Close();

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

        response.Close();
      }

Output from the Console.writeline for an example file:

buffer.length:1152

buffer.length:1126

buffer.length:1152

buffer.length:1152

buffer.length:1152

buffer.length:1152

buffer.length:1024

I've also used the exact copy from msdn (https://msdn.microsoft.com/en-us/library/ms229715(v=vs.110).aspx) but this had the same result.

Edit: Also tried the following code: string filePath = @"myFilePath"; var fileName = Path.GetFileName(filePath); var request = (FtpWebRequest)WebRequest.Create("ftp://myftp");

        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Credentials = new NetworkCredential(username, password);
        request.UsePassive = true;
        request.UseBinary = true;
        request.KeepAlive = false;

        using (var fileStream = File.OpenRead(filePath))
        {
            using (var requestStream = request.GetRequestStream())
            {
                fileStream.CopyTo(requestStream);
                requestStream.Close();
            }
        }

It gives the same result. The file is fully copied but every 512 bytes a newline is added.

With FileZilla I can do a correct FTP transfer of the same type of files.

1

There are 1 answers

0
DeGoosseZ On BEST ANSWER

I solved my problem using the build-in windows FTP that is accessible using the cmd.

            string[] lines = { username, password, "remote directory", "put " + "\"" + filePath + "\"" , "bye"};
        // WriteAllLines creates a file, writes a collection of strings to the file,
        // and then closes the file.  You do NOT need to call Flush() or Close().
        System.IO.File.WriteAllLines("full file path of your script", lines);
        Process cmd = new Process();
        cmd.StartInfo.FileName = "cmd.exe";
        cmd.StartInfo.RedirectStandardInput = true;
        cmd.StartInfo.RedirectStandardOutput = true;
        cmd.StartInfo.CreateNoWindow = true;
        cmd.StartInfo.UseShellExecute = false;
        cmd.Start();

        cmd.StandardInput.WriteLine(@"ftp -s:" + "\"" + "full file path of your script" + "\"" + " remote device");
        cmd.StandardInput.Flush();
        cmd.StandardInput.Close();
        cmd.WaitForExit();