Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

.net - Error while Uploading file to FTP server C#

I am trying to upload a .txt file to a ftp server. But i am having some issues. I keep getting this error: The remote server returned an error: (553) File name not allowed.

This always happens on this line:

Stream writer = ftpReq.GetRequestStream();

I cant figure out what the problem is. I have tried uploading the file with filezilla and that works fine. I also have tried to copy the file destination url from the server and harcode it into my application, but i still get the same error. Is there any other ways of uploading a file to a ftp server??

this is my code:

string localFilePath = @"\fileprintdataGroupsOperationfileExportsfolder";
string archiveFilePath = @"\fileprintdataGroupsOperationfileExportsfolderArchive";
string logFilePath = @"C:UserslmyDesktopLogs";
string ftpServer = "ftp://my.server.no:21/home/pll332/tmp/";
private string logFileName = "" + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString();
string userName = "pll332";
string password = "password";

public Controller()
{
}

public void UploadFile()
{
    try
    {
        string[] files = Directory.GetFiles(localFilePath);
        foreach (string file in files)
        {
            string fileName = Path.GetFileName(file);
            FtpWebRequest ftpReq = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServer + fileName));
            ftpReq.Method = WebRequestMethods.Ftp.UploadFile;
            ftpReq.UsePassive = true;
            ftpReq.UseBinary = true;
            ftpReq.KeepAlive = true;
            ftpReq.Credentials = new NetworkCredential(userName.Normalize(), password.Normalize());

            FileInfo fileInfo = new FileInfo(localFilePath + @"" + fileName);
            FileStream fileStream = fileInfo.OpenRead();

            byte[] fileContent = new byte[fileInfo.Length];
            fileStream.Read(fileContent, 0, Convert.ToInt32(fileInfo.Length));

            Stream writer = ftpReq.GetRequestStream();
            writer.Write(fileContent, 0, fileContent.Length);
            fileStream.Close();
            writer.Close();
            FtpWebResponse response = (FtpWebResponse)ftpReq.GetResponse();

            AppendLogFile(response, "Uploaded Files: ", fileName);
            MoveToArchive(file, archiveFilePath + fileName);
        }

    }
    catch (Exception exception)
    {
        Console.WriteLine(exception.Message);
    }
}

Hope you guys can help me. thanks!

EDIT: I have tried this:

            string fileName = Path.GetFileName(file);
            UriBuilder uri = new UriBuilder();
            uri.Scheme = "ftp";
            uri.Host = "my.server.no";
            uri.Port = 21;
            uri.Path = "/home/username/tmp/";
            uri.UserName = "username";
            uri.Password = "password";
            FtpWebRequest ftpReq = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri.ToString() + fileName));
            ftpReq.Method = WebRequestMethods.Ftp.UploadFile;
            ftpReq.UsePassive = true;
            ftpReq.UseBinary = true;
            ftpReq.KeepAlive = true;
            ftpReq.Credentials = new NetworkCredential(userName.Normalize(), password.Normalize());

This gives me the same error......

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I finally found the reason for the error. It seems that the server i was trying to upload to was a linux server. I found a description in a forum of how to fix the issue. And this is was was written in the forum:

This may help for Linux FTP server.

So, Linux FTP servers unlike IIS don't have common FTP root directory. Instead, when you log on to FTP server under some user's credentials, this user's root directory is used. So FTP directory hierarchy starts from /root/ for root user and from /home/username for others. So, if you need to query a file not relative to user account home directory, but relative to file system root, add an extra / after server name. Resulting URL will look like:

ftp://servername.net//var/lalala

so when i changed my connection Uri from:

ftp://username:[email protected]:21/home/username/tmp/

to:

ftp://username:[email protected]:21//home/username/tmp/

(notice i have added an extra / before /home/username/tmp/)

then i worked!!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...