Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 断开连接后的FtpWebRequest.GetRequestStream()_.net_Ftp_Ftpwebrequest - Fatal编程技术网

.net 断开连接后的FtpWebRequest.GetRequestStream()

.net 断开连接后的FtpWebRequest.GetRequestStream(),.net,ftp,ftpwebrequest,.net,Ftp,Ftpwebrequest,我正在制作一个ftp实用程序的原型,它将保存用户的进度,并在他们的互联网断开连接时从起始位置重新上传给定的文件。(适用于连接速度较慢的客户端) 其思想是打开一个文件流和ftp流,并在内存块中写入ftp流。如果出现异常(即断开连接导致IOException),则写入ftp服务器的字节数将保存在日志文件中,并在启动时读取 如果事务被取消,这段代码非常有效,但是如果客户端要断开连接,那么服务器端的ftp流永远不会被清除-所以我收到 远程服务器返回错误:(550)文件不可用(例如,找不到文件,无法访问)

我正在制作一个ftp实用程序的原型,它将保存用户的进度,并在他们的互联网断开连接时从起始位置重新上传给定的文件。(适用于连接速度较慢的客户端)

其思想是打开一个文件流和ftp流,并在内存块中写入ftp流。如果出现异常(即断开连接导致IOException),则写入ftp服务器的字节数将保存在日志文件中,并在启动时读取

如果事务被取消,这段代码非常有效,但是如果客户端要断开连接,那么服务器端的ftp流永远不会被清除-所以我收到

远程服务器返回错误:(550)文件不可用(例如,找不到文件,无法访问)

重新请求给定文件的ftp流时。代码看起来像

     //Create FTP Web Request
     reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(builder.ToString()));
     reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
     reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
     reqFTP.UsePassive = true;
     reqFTP.UseBinary = false;
     reqFTP.KeepAlive = false;
     reqFTP.ContentLength = fileInf.Length;
     reqFTP.ReadWriteTimeout = 5000;
     reqFTP.Timeout = 5000;

     using (ProgressDialog progressDialog = new ProgressDialog())
     {
        progressDialog.backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
        progressDialog.backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
        progressDialog.backgroundWorker1.FileName = filename;
        progressDialog.ShowDialog();
     }
  }

  private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  {
     FTPBackgroundWorker worker = sender as FTPBackgroundWorker;

     //Get file progress (if user canceled or crashed)
     worker.NumBytesRead = GetFileProgress(worker.FileName);
     reqFTP.ContentOffset = worker.NumBytesRead;

     const int buffLength = 2048;
     byte[] buff = new byte[buffLength];
     int contentLen;

     using (worker.FileStream = fileInf.OpenRead())
     {
        worker.FileStream.Position = worker.NumBytesRead;
        worker.FTPStream = reqFTP.GetRequestStream(); //Exception occurs

        while (true)
        {
           bool throwException = false;
           if (worker.CancellationPending)
           {
              e.Cancel = true;
              break;
           }

           contentLen = worker.FileStream.Read(buff, 0, buffLength);
           if (contentLen == 0)
              break;

           //write file to ftp stream
           worker.FTPStream.Write(buff, 0, contentLen);
           worker.NumBytesRead += contentLen;

           //For testing purposes
           if (throwException)
              throw new Exception("user disconnected!");
           worker.ReportProgress((int)(((double)worker.NumBytesRead / fileInf.Length) * 100));
        }
        worker.FileStream.Close();
        worker.FTPStream.Close();
     }
  }

  void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  {
     FTPBackgroundWorker worker = sender as FTPBackgroundWorker;
     if (e.Error != null)
     {
        MessageBox.Show(String.Format("Error occured uploading {0}: {1}",worker.FileName, e.Error), "Error");
        if (worker.FileStream != null)
           worker.FileStream.Close();
        if (worker.FTPStream != null)
           worker.FTPStream.Close();

        if(worker.NumBytesRead > 0)
        {
           MessageBox.Show("Progress has been saved", "Notification");
           WriteToLogFile(worker.FileName, worker.NumBytesRead);
        }
     }
     else if (e.Cancelled)
     {
        if (worker.FileStream != null)
           worker.FileStream.Close();
        if (worker.FTPStream != null)
           worker.FTPStream.Close();

        MessageBox.Show("Upload Canceled", "Cancel");
        if (worker.NumBytesRead > 0 && MessageBox.Show("Would you like to save your upload progress?", "Notification", MessageBoxButtons.YesNo) == DialogResult.Yes)
           WriteToLogFile(worker.FileName, worker.NumBytesRead);
     }
     else
     {
        RemoveFromLogFile(worker.FileName);
        MessageBox.Show("Upload Complete", "Success");
     }
  }
我的问题是:有没有办法检查服务器端是否有一个句柄不释放文件路径并将其删除?还是我用错误的方法来解决问题


感谢

为什么不将超时减少到一个在连接尝试再次初始化之前过期的值,或者让用户等待尝试再次启动连接?

我认为这可能也是最好(也是唯一)的解决方案你好,我可以问一下,在ftp类中上载或下载时,您是如何实现进度的吗,这就像跟踪写入ftp流的字节数一样简单。我只是将其存储在日志中以供实现。worker.FTPStream.Write(buff,0,contentLen);worker.NumBytesRead+=contentLen;谢谢,如果我也需要上传,有什么变化吗?这个例子实际上只实现了上传,我会在有机会的时候上传源代码。这会很好,但是你会在哪里上传源代码,或者你能告诉我如何在下载的过程中更新源代码吗