1

是否有可能做到这一点?...

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*)

想法?

4

0 回答 0