簡體   English   中英

將文件上傳到FTP服務器C#時出錯

[英]Error while Uploading file to FTP server C#

我正在嘗試將.txt文件上傳到ftp服務器。 但是我有一些問題。 我不斷收到此錯誤: 遠程服務器返回錯誤:(553)不允許使用文件名。

這總是在以下行上發生:

Stream writer = ftpReq.GetRequestStream();

我不知道是什么問題。 我嘗試使用filezilla上傳文件,效果很好。 我也曾嘗試從服務器復制文件目標URL並將其編碼到我的應用程序中,但是我仍然遇到相同的錯誤。 還有其他將文件上傳到ftp服務器的方法嗎?

這是我的代碼:

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);
    }
}

希望你們能幫助我。 謝謝!

編輯:我已經嘗試過:

            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());

這給了我同樣的錯誤……

我終於找到了錯誤的原因。 看來我嘗試上傳到的服務器是Linux服務器。 我在論壇中找到有關如何解決此問題的說明。 這是在論壇上寫的:

這可能對Linux FTP服務器有所幫助。

因此,與IIS不同,Linux FTP服務器沒有通用的FTP根目錄。 而是,當您使用某些用戶的憑據登錄到FTP服務器時,將使用該用戶的根目錄。 因此,FTP目錄層次結構從root用戶的/ root /和其他人的/ home / username開始。 因此,如果您需要查詢的文件不是相對於用戶帳戶主目錄,而是相對於文件系統根目錄,請在服務器名稱后添加一個額外的/ 產生的URL如下所示:

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

所以當我從以下位置更改連接Uri時:

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

至:

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

(注意我已經增加了額外的/之前的/ home /用戶名/ tmp目錄/)

然后我工作了!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM