将文件上传到FTP服务器时出错C# [英] Error while Uploading file to FTP server C#

查看:1322
本文介绍了将文件上传到FTP服务器时出错C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将.txt文件上传到FTP服务器。但我有一些问题。我一直收到这个错误:远程服务器返回一个错误:(553)文件名不允许。



  Stream writer = ftpReq.GetRequestStream(); 

我无法弄清楚问题所在。我已经尝试使用filezilla上传文件,并且工作正常。我也尝试从服务器复制文件目标网址,并将其编码到我的应用程序中,但我仍然收到相同的错误。有没有其他方法可以将文件上传到FTP服务器?



这是我的代码:

  string localFilePath = @\\fileprint\data\Groups\Operation\fileExports\folder\; 
string archiveFilePath = @\\fileprint\data\Groups\Operation\fileExports\folder\Archive\;
字符串logFilePath = @C:\Users\lmy\Desktop\Logs;
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;
字符串密码=密码;
$ b $ public Controller()
{
}

public void UploadFile()
{
try
{
string [] files = Directory.GetFiles(localFilePath);
foreach(文件中的字符串文件)
{
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(响应,Uploaded Files:,fileName);
MoveToArchive(file,archiveFilePath + fileName);
}

}
catch(例外例外)
{
Console.WriteLine(exception.Message);
}
}

希望你们能帮助我。谢谢!



编辑:
我试过这个:

  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 =密码;
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());

这给了我同样的错误......

解决方案

我终于找到了错误的原因。似乎我试图上传的服务器是一台Linux服务器。我在论坛中找到了如何解决问题的说明。这是写在论坛:

这可能有助于Linux FTP服务器。



因此,与IIS不同的Linux FTP服务器没有共同的FTP根目录。相反,当您在某个用户的凭据下登录到FTP服务器时,将使用该用户的根目录。因此,FTP目录层次结构从/ root /开始用于root用户,并从/ home /用户名开始用于其他用户。
因此,如果您需要查询与用户帐户主目录不相关的文件,但相对于文件系统根目录,请在服务器名称后添加一个额外的 / 。结果URL如下所示:



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



所以当我改变了我的连接Uri:

  ftp:// username: password@my.server.no:21 / home / username / tmp / 

to:

  ftp://用户名:password@my.server.no:21 // home / username / tmp / 

(注意我在 / home / username / tmp / 之前添加了一个额外的 / / strong>)



然后我工作!!


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 = @"\\fileprint\data\Groups\Operation\fileExports\folder\";
string archiveFilePath = @"\\fileprint\data\Groups\Operation\fileExports\folder\Archive\";
string logFilePath = @"C:\Users\lmy\Desktop\Logs";
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......

解决方案

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:password@my.server.no:21/home/username/tmp/

to:

ftp://username:password@my.server.no:21//home/username/tmp/

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

then i worked!!

这篇关于将文件上传到FTP服务器时出错C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆