Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# : FTP upload buffer size

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");
        }
    }
like image 294
R.Vector Avatar asked Dec 26 '22 23:12

R.Vector


1 Answers

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.

like image 112
Eugene Mayevski 'Callback Avatar answered Dec 31 '22 14:12

Eugene Mayevski 'Callback