使用ftpwebrequest上传大文件不会完成

时间:2012-12-11 00:31:32

标签: c# ftp uploading ftpwebrequest large-file-upload

我设计的软件使用ftp将文件上传到我的服务器我正在使用ftpwebrequest进行所有上传。当上传文件700mb它上传大约500mbs然后停止,它上传较小的文件时工作正常较小的文件上传成功,但它只是想在大文件上正常工作。我在后台工作程序中完成了上传,该工作程序将上载进度报告给主客户端上的进度条。当后台工作程序完成时,它执行后台工作程序完成功能。后台工作程序完成函数执行但上传永远不会完成,进度条停留在65%左右,就像客户端刚停止上传并执行后台工作程序完成功能一样,就好像它已完成上传一样。在这里可能会出现什么问题,上传没有完成,这里没有出现在服务器上的文件是代表上传的代码

void UploadFileInBackground_DoWork(object sender,DoWorkEventArgs e)
{
        byte[] data;
        int packetsize = 1024 * 8;

        string Destination = UploadURI + cattext + "/" + ID + ".obj";
        string source = DialogBrower.FileName;
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(Destination);
        request.Credentials = new NetworkCredential("user", "pass");
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.UsePassive = true;
        request.UseBinary = true;
        request.KeepAlive = false;
        using (FileStream fs = new FileStream(source, FileMode.Open, FileAccess.Read))
        {
            try
            {
                long filesize = fs.Length;
                long sum = 0;
                int count = 0;
                data = new byte[packetsize];
                Stream reqStream = request.GetRequestStream();
                float totalpackits = filesize / packetsize;
                float weightofpackit = 100 / totalpackits;
                float percentage = 0;
                while (sum < filesize)
                {
                    List<string> statusparms = new List<string>();
                    count = fs.Read(data, 0, packetsize);
                    reqStream.Write(data, 0, count);
                    sum += count;
                    percentage += weightofpackit;
                    int percentagetotal = Convert.ToInt32(Math.Round(percentage));
                    statusparms.Add(sum.ToString());
                    statusparms.Add(filesize.ToString());
                    UploadFileInBackground.ReportProgress(percentagetotal, statusparms);
                }
                reqStream.Close();
                uploadedname = uploadingname;
            }
            finally
            {
                fs.Dispose();
                data = null;
            }
        }
}

2 个答案:

答案 0 :(得分:0)

请改为尝试:

request.UseBinary = false;

答案 1 :(得分:0)

让我们试试这个

request.KeepAlive = false;

request.KeepAlive = true;