我正在嘗試使用以下代碼上傳檔案。但我收到錯誤訊息找不到路徑 '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'.
uj5u.com熱心網友回復:
您的 URI 字串被視為相對字串并與默認值結合使用BaseAddress(請參閱UploadFile 上的備注)
將其更改為絕對 URI 應該可以解決它(可能是ftp://ftp3.test.com/test123.txt)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/398770.html
