我用谷歌搜索了这个并遇到了很多教程,这些教程很好,但我收到一个小错误“远程服务器返回错误:(550)文件不可用(例如,找不到文件,无法访问)。”
这是我的代码
try
{
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + "XXX.XXX.XX.XXX/array1/Despatch");
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential("UserName", "Password");
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["DespatchExpressData"]));
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
}
catch (WebException e)
{
String status = ((FtpWebResponse)e.Response).StatusDescription;
}
在调用上述方法之前,会创建一个文本文件并将其保存在我的项目解决方案中名为 DispatchDetails 的文件夹中,该 txt 文件称为 Data.txt 我使用 StreamReader 获取 txt 文件,然后尝试将其上传到 FTP 上的文件夹帐户
我什至尝试对流式阅读器部分进行硬编码,但仍然出现错误:(
它落在这条线上
Stream requestStream = request.GetRequestStream();
任何帮助将不胜感激!
谢谢你。