FtpWebRequest returns error 550 File unavailable
Asked Answered
C

16

32

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();
Contrabass answered 4/7, 2013 at 13:37 Comment(0)
A
42

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

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

Annadiane answered 4/7, 2013 at 14:14 Comment(4)
Hi again. Thanks. The actual error message is 550 Access denied. I have given my folder full permission except 'Special permission'. Is there another possible cause besides 'special permission' ?Contrabass
Hi guys. I didn't win. I decided to host this app on another machine. It works fine. Thanks.Contrabass
This error may occur if you dont have enough permission or the directory in request is not present. like files in xxx.xxx.xxx.xxx/filesByblow
What a helpful tip, I was catching the "Exception" event only, now I can clearly see what the error stands for! ThanksEncage
C
31

Try this: 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.

Campion answered 15/12, 2014 at 15:10 Comment(5)
This is so horrible but it actually worked for me - I'm appalled, and I suppose that should be a bug, but thanks.Goddess
Amazing! That actually solved the problem. Thank god you wrote that. Would have never guessed it in a million years.Muster
Also ran into the problem. Great tip, never would have tried that.Mystagogue
This is got to be a bug in the MS FTP executable. I saw consistent, random failures of longer file paths pushing to an IIS FTP endpoint. The above fixed them.Outreach
Yup, it was as simple as that...these types of errors are just eating up too much of my time, but thx for the great tip!Inlier
N
8

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.

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

enter image description here enter image description here enter image description here

Neutrino answered 9/7, 2013 at 17:7 Comment(2)
Hi francis. I will try your suggestion. I will let you know as soon as i come right. Thank youContrabass
Remember to set the permissions on child folders. You can set the permissions on the top most folder and inherit these permissions down to child folders by: go to top level ftp folder =>right click=>properties=>security => advanced => pick a user => change permissions => edit permissions (as required) AND "include inheritable permissions from the object's parent"Despond
G
4

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.

Godwin answered 3/4, 2014 at 20:31 Comment(1)
Well spotted! I had this problem myself and had a look at the links provided here but they didn't help. Anyhow, the reason for the comment is because the official Microsoft documentation is incorrect. I followed this example: msdn.microsoft.com/en-us/library/ms229715(v=vs.110).aspx The request address is suffixed with a ".htm" extension and a text file is uploaded. I just thought the page was an error rather than the extension type. Beware anyone else that follows the above Microsoft example! Regards, MattKraigkrait
A
2

The server usually returns a relevant error message with the 550 code. But the FTP implementation in .NET framework translates all FTP status codes to its own (localized) message. Particularly code 550 is translated to "File unavailable". That, in many cases, hides away the real problem.

Enable .NET logging to see the real error message from the server:
Output log using FtpWebRequest


Here are some various common scenarios that lead to 550 error:

Atlantis answered 12/10, 2020 at 6:6 Comment(0)
O
1

I found the solution. The problem is the Current Working Directory of the ftp user. If you type a url like ftp:///path/test.txt it is used as a relative path ro the working directory. For example CWD is /test then the used path is /test/path/test.txt. If you want to use an absolute path, you have to type the url like ftp:////path/test.txt. Then the file is uploaded in the folder /path/test.txt, without exception.

Orphism answered 1/10, 2018 at 7:16 Comment(0)
B
0

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

Bierce answered 6/11, 2013 at 21:44 Comment(0)
F
0

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
            }
        }
Footwork answered 31/7, 2017 at 12:30 Comment(0)
T
0

I had this problem. Filezilla was fine, .net wasn't. It was to a wordpress server. To get it working, I changed the my code from this:

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

to:

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

and it now thankfully works.

I don't know if this is specific to Wordpress FTP folders.

Toponymy answered 25/10, 2017 at 9:43 Comment(0)
P
0

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/

Pissed answered 16/3, 2018 at 18:4 Comment(0)
V
0

I was also having the same issue. After monitoring the traffic with WireShark I understood the problem

I had set up a new FTP site on my local IIS FTP server named 'testftp' The 'testftp' site was pointing to a folder named d:\ftp on my computer's hard disk I was getting the '550' error whenever i tried to execute the line in bold in C# code below ftpHostURL="ftp://127.0.0.1/testftp/test.htm"; request = (FtpWebRequest)WebRequest.Create(ftpHostURL); request.Proxy = new WebProxy(); //-----Proxy bypassing(The requested FTP command is not supported when using HTTP proxy)

            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
           // uploadFilePath = "d://test//tt.txt";
            using (var requestStream = request.GetRequestStream())
            {
                using (var input = File.OpenRead(uploadFilePath))
                {
                    input.CopyTo(requestStream);
                }
            }

I tried a lot of solutions mentioned in various websites but nothing helped. Then I came across a site that suggested to use WireShark to monitor the traffic.

On monitoring the traffic using the RawCap Utility I saw that the application was trying to execute the code below

STOR testftp/test.htm

Then I understood the problem. IIS was expecting a folder named 'testftp' to be there under the ftp root folder which was not there. I was considering 'testftp' as the name of the virtual folder created in IIS where as IIS understood it as a folder under the FTP root

Creating a folder named 'testftp' under the FTP root folder solved my issue.

Hope this is helpful to someone

Regards Mathew

Verrazano answered 5/11, 2019 at 5:49 Comment(0)
C
0

the problem is that you try upload file that exists or a directory that exists, generate a exception, that no is a error.

Cholecalciferol answered 10/11, 2023 at 12:37 Comment(1)
Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?Hubbell
A
-1

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.

Arst answered 16/11, 2015 at 1:50 Comment(0)
P
-1

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

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

Peking answered 15/9, 2016 at 9:14 Comment(0)
F
-1

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"

Flung answered 11/1, 2017 at 15:56 Comment(0)
H
-1

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.

Hydrostatics answered 1/2, 2017 at 13:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.