如何编写C#代码以提高ftp文件上传性能 [英] how to write c# code to increase the ftp file upload performance

查看:100
本文介绍了如何编写C#代码以提高ftp文件上传性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经编写了一个C#代码以使用ftp上传文件.对于较小的文件来说还可以,但是对于较大的文件,性能却很差.如何解决这个问题.

需要示例代码.

以下是我的代码

Hi,

I have written a C# code to upload a file using ftp. For smaller file its ok, but large files the performance was very poor. How to resolve this.

Sample code required.

The following is my code

string result = "";
            FileInfo fileInf = new FileInfo(Frm_filename);
            FtpWebRequest reqFTP;
            //string s = "Accounts Data/To CPSMS";
            // Create FtpWebRequest object from the Uri provided
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ServiceProps.ftpServerIP + "/" + To_filename + "/" + fileInf.Name));

            reqFTP.Credentials = new NetworkCredential(ServiceProps.ftpUserID, ServiceProps.ftpPassword);


            reqFTP.KeepAlive = false;
            
            // Specify the command to be executed.
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
            
            // Specify the data transfer type.
            reqFTP.UsePassive = true;
            
            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 = 204800;
            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, Convert.ToInt32(buffLength));

                // Till Stream content ends
                int i = 0;
                while (contentLen != 0)
                {
                    i++;
                    // 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();
                result = "Y";
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }
            return result;



关于



Regards

推荐答案

引起我注意的第一件事是微观缓冲区大小.至少达到16K.尝试增加此大小以上的缓冲区,看看它是否有效.否则,它可能会受到服务器的限制(例如,大部分时间它都超载了);而且您不能从客户端更快地完成它.

—SA
First thing which catches my eye is microscopic buffer size. Make at least 16K. Experiment with increasing of buffer above this size and see if it works and helps. In not, it can be limited by the server (for example it''s over-loaded most of the time); and you cannot do it faster from the client side.

—SA


这篇关于如何编写C#代码以提高ftp文件上传性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆