【问题标题】:Uploading a file to a ftp server fails将文件上传到 ftp 服务器失败
【发布时间】:2013-11-19 08:03:42
【问题描述】:

我有一个小的 C# winform,我在其中生成一些文本文件,然后将它们移动到 ftp 服务器。 当我尝试将它们移动到生产服务器时,它在

下失败

远程服务器返回错误:(530) 未登录。

如果我通过 cmd/ftp 使用相同的 ftp 地址、用户名和密码登录 ftp,一切正常。我还在我的机器上安装了一个本地 ftp 服务器并对其进行了测试,看看我的代码是否会产生错误,但在本地它就像一个魅力,我只有生产 ftp 服务器有问题。 下面是我连接文件并将文件上传到 ftp 服务器的代码:

string[] FileName = Directory.GetFiles(outputpath);

foreach (string txtFile in FileName)
{
     FileInfo toUpload = new FileInfo(txtFile);

     FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + tbFTPAddress.Text + @"//" + toUpload.Name);

     request.Credentials = new NetworkCredential(tbFTPUserName.Text.Trim(), tbFTPPassword.Text.Trim());

     request.Method = WebRequestMethods.Ftp.UploadFile;

     Stream ftpStream = request.GetRequestStream();
     FileStream file = File.OpenRead(txtFile);

     int length = 1024;
     byte[] buffer = new byte[length];
     int bytesRead = 0;

     try
     {
         do
         {
             bytesRead = file.Read(buffer, 0, length);
             ftpStream.Write(buffer, 0, bytesRead);
         }
         while (bytesRead != 0);

         file.Close();
         ftpStream.Close();
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "Error encountered!", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
     finally
     {
         if (file != null) file.Close();
         if (ftpStream != null) ftpStream.Close();
     }
}

错误来自:Stream ftpStream = request.GetRequestStream();

有什么想法吗?

谢谢!

【问题讨论】:

  • 我应该补充一点,密码包含 * 字符。这会是个问题吗?
  • 您是否尝试过使用不同的外部 ftp 服务器?

标签: c# ftp stream filestream ftpwebrequest


【解决方案1】:

您必须首先调用 GetResponse()。

        FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(uri);
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Credentials = new NetworkCredential(Username, Password);

        try
        {
            //You have to call this or you would be unable to get a stream :)
            WebResponse response = fwr.GetResponse();
        }
        catch (Exception e)
        {
            throw e;
        }

        FileStream fs = new FileStream(localfile), FileMode.Open);
        byte[] fileContents = new byte[fs.Length];
        fs.Read(fileContents, 0, Convert.ToInt32(fs.Length));
        fs.Flush();
        fs.Close();

        //Now you are able to open a Stream
        Stream requestStream = request.GetRequestStream();
        requestStream.Write(fileContents, 0, fileContents.Length);
        requestStream.Close();

        request.Abort();

【讨论】:

  • 您好 Marcel,感谢您的反馈,但现在,即使在我的本地 ftp 服务器上,它在The remote server returned an error: (550) File unavailable (e.g., file not found, no access). Stream ftpStream = request.GetRequestStream(); 下也失败了,这是根据您的建议修改的代码:request.Method = WebRequestMethods.Ftp.UploadFile; try { WebResponse response = request.GetResponse(); } catch (Exception ex) { throw ex; } Stream ftpStream = request.GetRequestStream(); { throw ex; } Stream ftpStream = request.GetRequestStream(); FileStream file = File.OpenRead(txtFile);
  • 另外,让我在本地再次添加,代码可以工作并创建/移动文件。所以我认为问题不在于缺少 GetResponse() 调用。
  • 听起来像一个奇怪的问题:)。远程 FTP 是否以相同的设置运行?如果没有,请检查您的配置。可能远程服务器使用 sftp 或有其他限制。
  • 相同设置是什么意思?不幸的是,我试图将文件移动到的生产 ftp 服务器属于合作伙伴公司,除了他们已经提供给我们的用户名和密码外,我无法获得任何其他信息。如果它确实是一个 SFTP 服务器,它会有什么不同吗?谢谢!
【解决方案2】:

我也有这个错误。 (您不需要先获得响应。)在我的情况下,这是 FTP 服务器上的文件夹权限问题。

  1. 远程访问您的 FTP 服务器
  2. 导航到文件夹/子文件夹并右键单击
  3. 选择属性
  4. 切换到安全标签
  5. 点击编辑按钮
  6. 确保 IIS 用户帐户具有写入权限

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    • 2014-10-17
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多