C# FTP、GetResponse()、错误 550 文件不可用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17471745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:34:41  来源:igfitidea点击:

FTP, GetResponse (), error 550 File unavailable

c#.netfile-uploadftp

提问by chosenOne Thabs

I have created a small windows forms application to upload the file to one of our client's ftp site. But the problem that I'm having is that when I run this application on my local machine it uploads the file successfully. But if I run this program on our server, I get this error message;

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

remote server returned an error: (550) File unavailable (eg, file not found, can not access the file), on this line 'objFTPRequest.GetRequestStream();'.

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

Does anybody know why? Do I need to configure the firewall or something? Here is my code;

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

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();

回答by Microsoft DN

This error can be caused because of several reasons like file is not present on server, security permissions on file etc. etc.

导致此错误的原因有很多,例如服务器上不存在文件、文件的安全权限等。

First you need to find out the exact cause of error. This can be achieved by using following code-

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

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

Once you get the exact cause of error, you can go forward to solve it.

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

Here are some links you can refer

这里有一些链接你可以参考

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

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-e-g-file-not-found-no-access/

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://www.dreamincode.net/forums/topic/76361-file-upload-to-server/

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

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

回答by francis

I met the same problem, and this is what I did:

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

  1. Check the OS has the right to write. Find the ftp folder =>right click=>properties=>security, then you must know what you should do
  2. Check the ftp server open the write right to the user you logged. Open IIS=>click the ftp site=>ftp Authorization Rules=>add allow rules=>choose a user or group to set the rights
  3. Check the the dir on the ftp server, do the same thing on item 2.
  1. 检查操作系统是否有权写入。找到ftp文件夹=>右键=>属性=>安全,那你一定知道该怎么做了
  2. 检查ftp服务器打开你登录用户的写权限。打开IIS=>点击ftp站点=>ftp授权规则=>添加允许规则=>选择一个用户或组来设置权限
  3. 检查 ftp 服务器上的目录,对第 2 项执行相同的操作。

I can use four pictures to show the rights to be set: enter image description here

我可以用四张图来显示要设置的权限: 在此处输入图片说明

enter image description hereenter image description hereenter image description here

在此处输入图片说明在此处输入图片说明在此处输入图片说明

回答by Taras Kravets

In order to resolve this issue, it is required to force the System.Net.FtpWebRequest command to revert back to the old behavior of how it used to work in .Net Framework 2.0/3.5 and issue the extra CWD command before issuing the actual command.

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

In order to do this, the following code needs to be placed before any instance of the System.Net.FtpWebRequest class is invoked. The code below only needs to be called once, since it changes the settings of the entire application domain.

为此,需要在调用 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

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

回答by makemoney2010

It could be more simple.

它可以更简单。

I facing similar issue and i tried all the suggested solution but no one work. I'm figure out in simple manner like this one : take a look

我面临类似的问题,我尝试了所有建议的解决方案,但没有一个奏效。我以这样的简单方式弄清楚:看一看

Wrong code at my end

我最后的代码错误

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

Change it in this simple one :

在这个简单的改变它:

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

then procced with fill request and send to server :)

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

That's all.

就这样。

Make sure that all permission on server work fine and u're using right credential.

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

回答by Bremaria

Try this: ftp://xxx.xxx.xx.xx:21//path/filename

试试这个: ftp://xxx.xxx.xx.xx:21//path/filename

The "//" after the server address starts you out at the root directory. Even though I had: ftp://xxx.xxx.xx.xx:21/path/filename, it didn't take me to the correct directory.

服务器地址后的“//”将您引导至根目录。即使我有: ftp://xxx.xxx.xx.xx:21/path/filename,它也没有带我到正确的目录。

回答by dellyjm

Just to throw my hat in the ring, I was getting the same error. When the FTPRequest was requesting files from the FTP Service on the same computer (same IP). In the request I was using the IP address of the machine but once I changed that to 127.0.0.1 it worked. interestingly enough, requests from other IP addresses were being processed and files downloaded, just not from itself.

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

Hope that helped someone.

希望对某人有所帮助。

回答by Shrikant Dandile

When i had the same issue i tried everything above and after a day later i realize that the path which i created for uri having a white space in between "/" and the folder name

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

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

the above string should be

上面的字符串应该是

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

this small mistake also throws the same error and it's a valid error because this is not valid path for our file if it contains any white spaces between "/" and folder/file name

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

回答by shankar

I was having the same problem, when i compared with the ftpuri and User Name Path it is resolved When I create ftp access i have chose the path as /directory name

我遇到了同样的问题,当我与 ftpuri 和用户名路径进行比较时,它解决了当我创建 ftp 访问时,我选择了路径作为 /directory name

in the ftpuri when i included the directory name it gave the error and when i removed the directory name it is resolved.

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

ftpuri with error

ftpuri 有错误

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

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

ftpuri resolved

ftpuri 已解决

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

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

ftp access folder "/Project"

ftp 访问文件夹“/Project”

回答by user2709214

I too had this problem recently. What I found is that when I use ftpUploadFile the routine is trying to put the file in the root ftp folder. Normal command line ftp worked fine. However issuing a pwd command from the command line ftp revealed that this particular server was setting a different current directory upon login. Altering my ftpUploadFile to include this folder resolved the issue.

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

回答by Jan Machá?ek

Make sure target folder "outbox" exists. That was my problem with error 550.

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

Simply create target directory "output" before upload.

只需在上传前创建目标目录“输出”。

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
            }
        }