取消已上传的WebClient FTP上传

时间:2012-07-14 01:52:59

标签: c# ftp webclient

是否可以这样做?...

WebClient client;
using (WebClient client = new WebClient())
{
  client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressChanged);
  client.UploadFileCompleted += new UploadFileCompletedEventHandler(client_UploadFileCompleted);

  //Start the upload
  client.UploadFileAsync(new Uri("/targetfile.jpg"), @"c:\sourcefile.jpg");
}

private void client_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e)
{
  //upload finished
}

private void client_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
{
  if (e.ProgressPercentage == 60)
  {
    //Cancel the upload
    client.CancelAsync();
  }

  //update progressbar
}

当我尝试使用CancelAsync()取消上传时,我发现我的应用程序终止(没有例外或错误......当我从IDE运行时关闭。)但文件继续上传到FTP站点(我可以看到它监控FTP站点)。

旁注...当我将其作为已编译的应用程序运行时,它也会崩溃,没有任何类型的错误,但检查应用程序下的系统日志我得到.Net运行时错误......

Application: MyUploader.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Net.WebException
Stack:
   at System.Net.FtpDataStream.System.Net.ICloseEx.CloseEx(System.Net.CloseExState)
   at System.Net.FtpDataStream.Dispose(Boolean)
   at System.IO.Stream.Close()
   at System.Net.WebClient+UploadBitsState.Close()
   at System.Net.WebClient.UploadBitsWriteCallback(System.IAsyncResult)
   at System.Net.LazyAsyncResult.Complete(IntPtr)
   at System.Net.ContextAwareResult.CompleteCallback(System.Object)
   at System.Threading.ExecutionContext.runTryCode(System.Object)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode, CleanupCode, System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Net.ContextAwareResult.Complete(IntPtr)
   at System.Net.LazyAsyncResult.ProtectedInvokeCallback(System.Object, IntPtr)
   at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
   at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)

想法?

0 个答案:

没有答案