4

I have go that FTP Upload function , but there is something that i want to ask about It is the Buffer size , i set it to 20KB what does it mean and would it make difference if i increased/decreased it ?

    private void Upload(string filename)
    {
        FileInfo fi = new FileInfo(filename);

        FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create("ftp://" + textBox1.Text + "/" + Path.GetFileName(filename));
        ftp.Credentials = new NetworkCredential(textBox2.Text, textBox3.Text);
        ftp.Method = WebRequestMethods.Ftp.UploadFile;
        ftp.UseBinary = true;
        ftp.KeepAlive = false;
        ftp.ContentLength = fi.Length;

        // The buffer size is set to 20kb
        int buffLength = 20480;
        byte[] buff = new byte[buffLength];
        int contentLen;

        //int totalReadBytesCount = 0;

        FileStream fs = fi.OpenRead();

        try
        {
            // Stream to which the file to be upload is written
            Stream strm = ftp.GetRequestStream();

            // Read from the file stream 2kb at a time
            contentLen = fs.Read(buff, 0, buffLength);

            // Till 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)
        {
            MessageBox.Show(ex.Message, "Upload Error");
        }
    }
R.Vector
  • 1,669
  • 9
  • 33
  • 41

2 Answers2

10

For FTP on desktop systems block size of about 256Kb produced the best performance in our tests. Small buffer sizes decrease speed of transfer significantly. I recommend that you do some measurements yourself, but 20Kb is definitely too little for a buffer.

Eugene Mayevski 'Callback
  • 45,135
  • 8
  • 71
  • 121
0

Files are already buffered by the file system cache. You should use something lower than 20KB. 4 KB is a traditional choice and I really wouldn't go lower than 4 KB. Don't go below a kilobyte, more than 16 KB is a waste of memory and unfriendly to the CPU's L1 cache (typically 16 or 32 KB of data).

Hans (https://stackoverflow.com/a/3034155)

Use 4 KB (AKA 4096 b)

In .Net 4.5 they increased the default value to 81920 bytes and using the .Net Reflector shows the _DefaultCopyBufferSize has a value of 0x14000 (81920b, or 80K). However, this is for copying from stream to stream, not buffering data. The BufferedStream Class has a _DefaultBufferSize of 0x1000 (4096b or 4k).

Community
  • 1
  • 1
Arvo Bowen
  • 4,524
  • 6
  • 51
  • 109
  • Could you explain the reason why a buffer below 20KB should be chosen? It makes the transfer very slow! – Toshi Dec 07 '16 at 09:07
  • Even microsoft defaults it to 4KB. https://msdn.microsoft.com/en-us/library/system.io.bufferedstream(v=vs.110).aspx - If use use the wrapped up MS `FtpPutFile()` you in fact would be using a buffer size of 4KB. Not sure why my answer would be wrong (and you would fell the need to downvote) if it's something that is built into the framework ... – Arvo Bowen Dec 07 '16 at 12:42
  • Also added the source of my answer. A lot of others seem to agree with me on that answer. - As a side note I also send small files as well (40KB or so) and that work VERY well for me. If you have larger files your going to want to use a larger buffer for best performance I would think. – Arvo Bowen Dec 07 '16 at 12:45
  • No problem @Toshi. ;) – Arvo Bowen Dec 07 '16 at 16:30