【问题标题】:Uploading folder to ftp subfolder returns error (550) file unavailable, no access将文件夹上传到 ftp 子文件夹返回错误 (550) 文件不可用,无法访问
【发布时间】:2010-12-06 18:03:34
【问题描述】:

我正在尝试将图像上传到 ftp。我需要将它放在一个以特定名称调用的压缩文件夹中,然后将该文件夹上传到特定目录。每次尝试,我都会收到错误远程服务器返回错误:(550)文件不可用 当我尝试一次上传一张图片时,此代码可以正常工作。在这里,我正在尝试上传整个文件夹。我检查了uri(我从调试中复制了它),它去那里就好了。我必须以不同的方式上传文件夹吗?我认为这是一个写权限问题,但我可以手动登录并将文件夹上传到正确的位置。然后我尝试获取我能够的目录列表。我也无法将文件夹上传到根目录。我很绝望!我什至不知道在哪里谷歌!

 string ftpPassword = ConfigurationManager.AppSettings["ftpPassword"].ToString();
  string uri = remoteDirectory;
  FileInfo fileInf = new FileInfo(FileToUpload);
  // Create FtpWebRequest object from the Uri provided
  FtpWebRequest reqFTP = null;
  reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  reqFTP.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
  reqFTP.KeepAlive = false;
  reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
  // Specify the data transfer type.
  reqFTP.UseBinary = true;
  // Notify the server about the size of the uploaded file
  reqFTP.ContentLength = fileInf.Length;
  // The buffer size is set to 2kb
  int buffLength = 2048;
  byte[] buff = new byte[buffLength];
  int contentLen;
  // open file to be uploaded
  using (FileStream fs = fileInf.OpenRead())
  {
  try
  {
  // Stream to which the file to be upload is written
  using (Stream strm = reqFTP.GetRequestStream())
  {
  // Read from the file stream 2kb at a time till Stream content ends
  contentLen = fs.Read(buff, 0, buffLength);
  while (contentLen != 0)
  {
  // Write Content from the file stream to the FTP Upload Stream
  strm.Write(buff, 0, contentLen);
  contentLen = fs.Read(buff, 0, buffLength);
  }
  }
  reqFTP = null;
  ////Update the database with the new image location and delete the img from the uploadedimages folder
  //DataAccess.UpdateImageDB(item.ProductID, item.ImgFolder + "/" + item.IMG);
  System.IO.File.Delete(fileInf.ToString());
  }
  {
  Console.WriteLine(ex.Message, "Upload Error");
  }

【问题讨论】:

    标签: .net ftpwebrequest


    【解决方案1】:

    您可能需要检查目录(文件夹)是否存在,如果不存在,则需要在 FTP 上创建它。我很确定大多数 FTP 客户端都会为您执行此操作。

    【讨论】:

    • 我知道该文件夹存在。我正在从数据库中提取文件夹名称。此外,当我逐步浏览它时,我将 uri 复制并粘贴到浏览器中,它没有问题。
    • 当我使用 filezilla 登录我的 ftp 时,我注意到所有者/组说没有人轮。这和它有什么关系吗?
    • 可能是文件夹的权限问题。
    【解决方案2】:

    我必须进入 NTFS 权限,并将具有“完全控制权”的 IUSR 添加到 ftp 文件夹。

    【讨论】:

      【解决方案3】:

      为了解决这个问题,需要强制 System.Net.FtpWebRequest 命令恢复到以前在 .Net Framework 2.0/3.5 中的工作方式,并在发出之前发出额外的 CWD 命令实际的命令。

      为此,需要在调用 System.Net.FtpWebRequest 类的任何实例之前放置以下代码。下面的代码只需要调用一次,因为它改变了整个应用程序域的设置。

      private static void SetMethodRequiresCWD()
      {
          Type requestType = typeof(FtpWebRequest);
          FieldInfo methodInfoField = requestType.GetField("m_MethodInfo", BindingFlags.NonPublic | BindingFlags.Instance);
          Type methodInfoType = methodInfoField.FieldType;
      
      
          FieldInfo knownMethodsField = methodInfoType.GetField("KnownMethodInfo", BindingFlags.Static | BindingFlags.NonPublic);
          Array knownMethodsArray = (Array)knownMethodsField.GetValue(null);
      
          FieldInfo flagsField = methodInfoType.GetField("Flags", BindingFlags.NonPublic | BindingFlags.Instance);
      
          int MustChangeWorkingDirectoryToPath = 0x100;
          foreach (object knownMethod in knownMethodsArray)
          {
              int flags = (int)flagsField.GetValue(knownMethod);
              flags |= MustChangeWorkingDirectoryToPath;
              flagsField.SetValue(knownMethod, flags);
          }
      }
      

      http://support.microsoft.com/kb/2134299

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-21
        • 2013-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多