FTP,GetResponse(),错误550文件不可用

我创建了一个小的Windows窗体应用程序,将文件上传到我们客户端的ftp站点之一。 但我遇到的问题是,当我在本地计算机上运行此应用程序时,它会成功上传文件。 但是,如果我在我们的服务器上运行此程序,我会收到此错误消息;

远程服务器返回错误:(550)文件不可用(例如,找不到文件,无法访问该文件),在此行’objFTPRequest.GetRequestStream();’。

有人知道为什么吗? 我需要配置防火墙吗? 这是我的代码;

FileInfo objFile = new FileInfo(filename); FtpWebRequest objFTPRequest; // Create FtpWebRequest object objFTPRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/outbox/" + objFile.Name)); // Set Credintials objFTPRequest.Credentials = new NetworkCredential(ftpUserName, ftpPassword); // By default KeepAlive is true, where the control connection is // not closed after a command is executed. objFTPRequest.KeepAlive = false; // Set the data transfer type. objFTPRequest.UseBinary = true; // Set content length objFTPRequest.ContentLength = objFile.Length; // Set request method objFTPRequest.Method = WebRequestMethods.Ftp.UploadFile; // Set buffer size int intBufferLength = 16 * 1024; byte[] objBuffer = new byte[intBufferLength]; // Opens a file to read FileStream objFileStream = objFile.OpenRead(); // Get Stream of the file Stream objStream = objFTPRequest.GetRequestStream(); int len = 0; while ((len = objFileStream.Read(objBuffer, 0, intBufferLength)) != 0) { // Write file Content objStream.Write(objBuffer, 0, len); } objStream.Close(); objFileStream.Close(); 

由于服务器上没有文件,文件安全权限等几个原因,可能会导致此错误。

首先,您需要找出错误的确切原因。 这可以通过使用以下代码来实现 –

 try { //Your code } catch(WebException e) { String status = ((FtpWebResponse)e.Response).StatusDescription; } 

一旦得到错误的确切原因,您可以继续解决它。

以下是您可以参考的一些链接

http://forums.asp.net/t/1777881.aspx/1

http://nickstips.wordpress.com/2010/10/25/c-ftp-upload-error-the-remote-server-returned-an-error-550-file-unavailable-eg-file-not-found-没有访问/

http://www.dreamincode.net/forums/topic/76361-file-upload-to-server/

http://forums.asp.net/t/1374306.aspx/1

试试这个: ftp://xxx.xxx.xx.xx:21 //路径/文件名

服务器地址后面的“//”在根目录下启动。 即使我有: ftp://xxx.xxx.xx.xx:21 / path / filename ,它也没有带我到正确的目录。

我遇到了同样的问题,这就是我所做的:

  1. 检查操作系统是否有权写。 找到ftp文件夹=>右键单击=> properties => security,然后你必须知道你应该做什么
  2. 检查ftp服务器,打开您记录的用户的写权限。 打开IIS =>单击ftp site => ftp授权规则=>添加允许规则=>选择用户或组来设置权限
  3. 检查ftp服务器上的目录,在第2项上执行相同的操作。

我可以使用四张图片来显示要设置的权限: 在此处输入图像描述

在此处输入图像描述在此处输入图像描述在此处输入图像描述

它可能更简单。

我面临类似的问题,我尝试了所有建议的解决方案,但没有人工作。 我像这样简单地弄清楚:看一看

我的代码错了

  Dim request As Net.FtpWebRequest = CType(FtpWebRequest.Create("ftp://xxx.xxx.xxx.xxx/files"), FtpWebRequest) 

在这个简单的改变它:

  Dim request As Net.FtpWebRequest = CType(FtpWebRequest.Create("ftp://xxx.xxx.xxx.xxx/files/youfilename.ext"), FtpWebRequest) 

然后用填充请求进行处理并发送到服务器:)

就这样。

确保服务器上的所有权限都正常工作,并使用正确的凭据。

为了解决此问题,需要强制System.Net.FtpWebRequest命令恢复到以前在.Net Framework 2.0 / 3.5中工作的旧行为,并在发出实际命令之前发出额外的CWD命令。

为此,需要在调用System.Net.FtpWebRequest类的任何实例之前放置以下代码。 下面的代码只需要调用一次,因为它会更改整个应用程序域的设置。

 private static void SetMethodRequiresCWD() { Type requestType = typeof(FtpWebRequest); FieldInfo methodInfoField = requestType.GetField("m_MethodInfo", BindingFlags.NonPublic | BindingFlags.Instance); Type methodInfoType = methodInfoField.FieldType; FieldInfo knownMethodsField = methodInfoType.GetField("KnownMethodInfo", BindingFlags.Static | BindingFlags.NonPublic); Array knownMethodsArray = (Array)knownMethodsField.GetValue(null); FieldInfo flagsField = methodInfoType.GetField("Flags", BindingFlags.NonPublic | BindingFlags.Instance); int MustChangeWorkingDirectoryToPath = 0x100; foreach (object knownMethod in knownMethodsArray) { int flags = (int)flagsField.GetValue(knownMethod); flags |= MustChangeWorkingDirectoryToPath; flagsField.SetValue(knownMethod, flags); } } 

http://support.microsoft.com/kb/2134299

当我遇到同样的问题时,我尝试了上面的所有内容,一天之后,我意识到我为uri创建的路径在“/”和文件夹名称之间有一个空白区域

 string uri="192.168.1.101/ Sync/Image.jpg"; 

上面的字符串应该是

 string uri="192.168.1.101/Sync/Image.jpg"; 

这个小错误也会抛出相同的错误并且它是一个有效的错误,因为如果它包含“/”和文件夹/文件名之间的任何空格,则这不是我们文件的有效路径

只是把我的帽子戴在戒指上,我得到了同样的错误。 当FTPRequest在同一台计算机(同一IP)上从FTP服务请求文件时。 在请求中,我使用的是机器的IP地址,但是一旦我将其更改为127.0.0.1就可以了。 有趣的是,来自其他IP地址的请求正在处理和下载文件,而不是自己。

希望有人帮助过。

我遇到了同样的问题,当我与ftpuri和用户名路径进行比较时,它被解析当我创建ftp访问时,我选择路径为/目录名称

在ftpuri中,当我包含目录名称时,它给出了错误,当我删除目录名称时,它就被解析了。

ftpuri有错误

ftp://www.example.com/Project/yourfilename.ds

ftpuri解决了

ftp://www.example.com/yourfilename.ds

ftp访问文件夹“/ Project”

我最近也遇到过这个问题。 我发现当我使用ftpUploadFile时,例程试图将文件放在根ftp文件夹中。 正常命令行ftp工作正常。 但是,从命令行ftp发出pwd命令显示该特定服务器在登录时设置了不同的当前目录。 更改我的ftpUploadFile以包含此文件夹解决了该问题。

确保目标文件夹“发件箱”存在。 这是我的错误550的问题。

只需在上传之前创建目标目录“output”。

 try { WebRequest request = WebRequest.Create("ftp://" + ftpServerIP + "/outbox"); request.Credentials = new NetworkCredential("user", "password"); request.Method = WebRequestMethods.Ftp.MakeDirectory; using (var resp = (FtpWebResponse)request.GetResponse()) { Console.WriteLine(resp.StatusCode); } } catch (WebException ex) { FtpWebResponse response = (FtpWebResponse)ex.Response; if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable) { response.Close(); //already exists } else { response.Close(); //doesn't exists = it's another exception } } 

我有这个问题。 Filezilla很好,.net不是。 这是一个wordpress服务器。 为了使它工作,我改变了我的代码:

ftp://XXX.XXX.XXX.XXX//folder//“+ txtFile.Text

至:

ftp:// [FTPNAME] @XXX.XXX.XXX.XXX // “+ txtFile.Text

现在谢天谢地。

我不知道这是否特定于Wordpress FTP文件夹。

在我的情况下,我的ftp地址中引用的根文件夹丢失了。 创建文件夹后,问题得以解决。

FTP://xxx.xxx.xx.xx:21 // rootfolder /

我找到了解决方案。 问题是ftp用户的当前工作目录。 如果键入类似ftp:///path/test.txt的URL,则将其用作工作目录的相对路径。 例如,CWD是/ test,然后使用的路径是/test/path/test.txt。 如果要使用绝对路径,则必须键入ftp:////path/test.txt之类的URL。 然后将文件上传到文件夹/path/test.txt中,无一例外。