通过FTP c#将txt文件上传到服务器上的文件夹

时间:2012-10-09 13:19:20

标签: c# ftp text-files

我搜索了这个,并遇到了很多教程,这很好,但我得到一个小错误“远程服务器返回错误:(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;
            }

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 im,使用StreamReader获取txt文件,然后尝试将其上传到文件夹在FTP帐户

我甚至尝试对流读取器部分进行硬编码并仍然得到错误:(

它落在这一行

Stream requestStream = request.GetRequestStream();

任何帮助都将受到高度赞赏!

谢谢。

1 个答案:

答案 0 :(得分:1)

看看here

可能您必须为WebRequest.Create设置正确的URI,例如

WebRequest.Create("ftp://" + "XXX.XXX.XX.XXX//array1/Despatch");

当然,检查一下,如果你有权写:)