.net core 3.1 WebClient上传Could not find a part of the path 错误

.net core 3.1 WebClient upload Could not find a part of the path error

我正在尝试使用以下代码上传文件。但是我收到错误消息 Could not find a part of the path 'C:\source\repos\FTPTest\FTPTest\bin\Debug\netcoreapp3.1\ftp3.test.com\test123.txt'

static void ftpuploadtest()
        {
            String uriString = "ftp3.test.com/test123.txt";
            WebClient myWebClient = new WebClient();
            myWebClient.Credentials = new NetworkCredential("username", "password");
            string fileName = "C:\Temp\unitssstest.txt"; 
            byte[] responseArray = myWebClient.UploadFile(uriString, fileName);
            Console.WriteLine("\nResponse Received. The contents of the file uploaded are:\n{0}",
                System.Text.Encoding.ASCII.GetString(responseArray));
            
        }

错误

System.Net.WebException
  HResult=0x80070003
  Message=Could not find a part of the path 'C:\source\repos\FTPTest\FTPTest\bin\Debug\netcoreapp3.1\ftp3.test.com\test123.txt'.
  Source=System.Net.Requests
  StackTrace:
   at System.Net.FileWebRequest.CreateWriteStream()
   at System.Net.FileWebRequest.<>c.<BeginGetRequestStream>b__55_0(Object s)
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.<>c.<.cctor>b__274_0(Object obj)
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at System.Threading.Tasks.TaskToApm.End[TResult](IAsyncResult asyncResult)
   at System.Net.FileWebRequest.EndGetRequestStream(IAsyncResult asyncResult)
   at System.Net.FileWebRequest.GetRequestStream()
   at System.Net.WebClient.UploadBits(WebRequest request, Stream readStream, Byte[] buffer, Int32 chunkSize, Byte[] header, Byte[] footer)
   at System.Net.WebClient.UploadFile(Uri address, String method, String fileName)
   at System.Net.WebClient.UploadFile(String address, String fileName)
   at FTPTest.Program.ftpuploadtest() in C:\source\repos\FTPTest\FTPTest\Program.cs:line 20
   at FTPTest.Program.Main(String[] args) in C:\source\repos\FTPTest\FTPTest\Program.cs:line 11

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
DirectoryNotFoundException: Could not find a part of the path 'C:\source\repos\FTPTest\FTPTest\bin\Debug\netcoreapp3.1\ftp3.test.com\test123.txt'.

您的 URI 字符串被视为相对字符串并与默认值 BaseAddress 合并(请参阅 UploadFile 上的备注)

将其更改为绝对 URI 应该可以解决它(可能 ftp://ftp3.test.com/test123.txt