ftpwebrequest - FTP FileUpload - 传输速率

时间:2018-01-17 14:25:43

标签: c# ftpwebrequest

我使用c#ftpwebrequest将10GB文件上传到我的ftp服务器。如何计算转移率?

以下是代码:

        FileStream fs = null;
        Stream rs = null;

        try
        {


            string uploadFileName = new FileInfo(file).Name;


            string uploadUrl = ftpServer;

            Console.WriteLine("Start Time: {0}", DateTime.Now);

            fs = new FileStream(file, FileMode.Open, FileAccess.Read);

            string ftpUrl = string.Format("{0}/{1}", uploadUrl, uploadFileName);
            FtpWebRequest requestObj = FtpWebRequest.Create(ftpUrl) as FtpWebRequest;

            requestObj.Method = WebRequestMethods.Ftp.UploadFile;

            rs = requestObj.GetRequestStream();

            byte[] buffer = new byte[40960];

            int read = 0;
            while ((read = fs.Read(buffer, 0, buffer.Length)) != 0)
            {
                rs.Write(buffer, 0, read);
            }
            rs.Flush();


        }
        catch (Exception ex)
        {
            Console.WriteLine("File upload/transfer Failed.\r\nError Message:\r\n" + ex.Message);
        }
        finally
        {
            if (fs != null)
            {
                fs.Close();
                fs.Dispose();
            }

            if (rs != null)
            {
                rs.Close();
                rs.Dispose();
            }
        }

        Console.WriteLine("End Time: {0}", DateTime.Now);

        Console.WriteLine("Exiting the application.. press any key to continue");
        Console.ReadLine();

上传速度很慢所以在参考了一些文章后我将缓冲区大小提升到了40960。速度有所提高。

由于我是初学者,请解释解决方案以及我的理解。提前谢谢。

0 个答案:

没有答案