FTP, GetResponse (), error 550 File unavailable

2019-01-09 11:41发布

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;

remote server returned an error: (550) File unavailable (eg, file not found, can not access the file), on this line '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();

13条回答
聊天终结者
2楼-- · 2019-01-09 12:17

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.

Hope that helped someone.

查看更多
男人必须洒脱
3楼-- · 2019-01-09 12:29

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

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

ftpuri with error

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

ftpuri resolved

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

ftp access folder "/Project"

查看更多
虎瘦雄心在
4楼-- · 2019-01-09 12:29

In my case, there was a root folder referenced in my ftp address that was missing. After to create the folder the problem was solved.

ftp://xxx.xxx.xx.xx:21//rootfolder/

查看更多
三岁会撩人
5楼-- · 2019-01-09 12:30

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.

查看更多
我命由我不由天
6楼-- · 2019-01-09 12:34

Make sure target folder "outbox" exists. That was my problem with error 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
            }
        }
查看更多
可以哭但决不认输i
7楼-- · 2019-01-09 12:35

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.

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.

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

查看更多
登录 后发表回答