Can't connect to FTP: (553) File name not allo

2019-01-18 05:13发布

I need to FTP a file to a directory. In .Net I have to use a file on the destination folder to create a connection so I manually put Blank.dat on the server using FTP. I checked the access (ls -l) and it is -rw-r--r--. But when I attempt to connect to the FTP folder I get: "The remote server returned an error: (553) File name not allowed" back from the server. The research I have done says that this may arrise from a permissions issue but as I have said I have permissions to view the file and can run ls from the folder. What other reasons could cause this issue and is there a way to connect to the folder without having to specify a file?

            byte[] buffer;
            Stream reqStream;
            FileStream stream;
            FtpWebResponse response;
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(string.Format("ftp://{0}/{1}", SRV, DIR)));
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential(UID, PASS);
            request.UseBinary = true;
            request.Timeout = 60000 * 2;
            for (int fl = 0; fl < files.Length; fl++)
            {
                request.KeepAlive = (files.Length != fl);
                stream = File.OpenRead(Path.Combine(dir, files[fl]));
                reqStream = request.GetRequestStream();
                buffer = new byte[4096 * 2];
                int nRead = 0;
                while ((nRead = stream.Read(buffer, 0, buffer.Length)) != 0)
                {
                    reqStream.Write(buffer, 0, nRead);
                }
                stream.Close();
                reqStream.Close();

                response = (FtpWebResponse)request.GetResponse();
                response.Close();
            }

标签: c# .net ftp
9条回答
▲ chillily
2楼-- · 2019-01-18 05:49

You must be careful with names and paths:

 string FTP_Server = @"ftp://ftp.computersoft.com//JohnSmith/";
 string myFile="text1.txt";
 string myDir=@"D:/Texts/Temp/";

if you are sending to ftp.computersoft.com/JohnSmith a file caled text1.txt located at d:/texts/temp

then

    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(FTP_Server+myFile);
    request.Method = WebRequestMethods.Ftp.UploadFile;                      

    request.Credentials = new NetworkCredential(FTP_User, FTP_Password);    

    StreamReader sourceStream = new StreamReader(TempDir+myFile);                  
    byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
    sourceStream.Close();

    Stream requestStream = request.GetRequestStream();
    requestStream.Write(fileContents, 0, fileContents.Length);              
    requestStream.Close();

notice that at one moment you use as destination

ftp://ftp.computersoft.com//JohnSmith/text1.txt

which contains not only directory but the new file name at FTP server as well (which in general can be different than the name of file on you hard drive)

and at other place you use as source

D:/Texts/Temp/text1.txt

查看更多
够拽才男人
3楼-- · 2019-01-18 05:53

I saw something similar to this a while back, it turned out to be the fact that I was trying to connect to an internal iis ftp server that was secured using Active Directory.

In my network credentials I was using new NetworkCredential(@"domain\user", "password"); and that was failing. Switching to new NetworkCredential("user", "password", "domain"); worked for me.

查看更多
做自己的国王
4楼-- · 2019-01-18 05:54

I hope this will be helpful for someone

if you are using LINUX server, replace your request path from

FtpWebRequest req= (FtpWebRequest)WebRequest.Create(@"ftp://yourdomain.com//yourpath/" + filename);

to

FtpWebRequest req= (FtpWebRequest)WebRequest.Create(@"ftp://yourdomain.com//public_html/folderpath/" + filename);

the folder path is how you see in the server(ex: cpanel)

查看更多
【Aperson】
5楼-- · 2019-01-18 05:59

your directory has access limit.
delete your directory and then create again with this code:

//create folder  
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create("ftp://mpy1.vvs.ir/Subs/sub2");
request.Method = WebRequestMethods.Ftp.MakeDirectory;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = true ;
using (var resp = (FtpWebResponse)request.GetResponse())
{

}
查看更多
对你真心纯属浪费
6楼-- · 2019-01-18 05:59

I had this problem when I tried to write to the FTP site's root directory pro grammatically. When I repeated the operation manually, the FTP automatically rerouted me to a sub-directory and completed the write operation. I then changed my code to prefix the target filename with the sub-directory and the operation was successful.

查看更多
爷的心禁止访问
7楼-- · 2019-01-18 06:01

Another reason for this error could be that the FTP server is case sensitive. That took me good while to figure out.

查看更多
登录 后发表回答