上传文件到FTP子返回错误(550)文件不可用,无法访问 [英] Uploading folder to ftp subfolder returns error (550) file unavailable, no access

查看:387
本文介绍了上传文件到FTP子返回错误(550)文件不可用,无法访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将图像上传到FTP。我需要有它称为一个特定的名称pssed COM $ P $文件夹,然后将该文件夹上传到指定目录。每次我尝试,我得到一个错误的远程服务器返回错误:(550)文件不可用 这code正常工作时,我尝试上传一个图像的时间。在这里,我想上传整个文件夹。我检查了URI(我在调试复制它),它去那里就好了。是否有不同的方式,我必须做的上传文件夹?我认为这是一个写权限的问题,但是我可以手动登录和文件夹上传到适当的位置。然后我试图获取目录列表而我能。我无法将文件夹上传到root身份。我是pretty的绝望!我甚至不知道在哪里,以谷歌!

 字符串ftpPassword = ConfigurationManager.AppSettings [ftpPassword]​​的ToString()。
  字符串的uri = remote目录;
  FileInfo的fileInf =新的FileInfo(FileToUpload);
  //创建的FtpWebRequest对象从乌里提供
  的FtpWebRequest reqFTP = NULL;
  reqFTP =(的FtpWebRequest)FtpWebRequest.Create(新的URI(URI));
  reqFTP.Credentials =新的NetworkCredential(ftpUsername,ftpPassword);
  reqFTP.KeepAlive = FALSE;
  reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
  //指定数据传输类型。
  reqFTP.UseBinary = TRUE;
  //通知服务器对上传文件的大​​小
  reqFTP.ContentLength = fileInf.Length;
  //缓冲区大小被设定为2KB
  INT buffLength = 2048;
  byte []的BUFF =新的字节[buffLength]
  INT contentLen;
  //要上传打开文件
  使用(的FileStream FS = fileInf.OpenRead())
  {
  尝试
  {
  //流至该文件被上传写入
  使用(流STRM = reqFTP.GetRequestStream())
  {
  //从文件中读取流2KB的时间,直到流内容结束
  contentLen = fs.Read(BUFF,0,buffLength);
  而(contentLen!= 0)
  {
  //将数据从文件内容传输到FTP上载流
  strm.Write(BUFF,0,contentLen);
  contentLen = fs.Read(BUFF,0,buffLength);
  }
  }
  reqFTP = NULL;
  ////更新与新的图像位置的数据库,并从uploadedimages文件夹中删除IMG
  //DataAccess.UpdateImageDB(item.ProductID,item.ImgFolder +/+ item.IMG);
  System.IO.File.Delete(fileInf.ToString());
  }
  {
  Console.WriteLine(ex.Message,上传错误);
  }
 

解决方案

为了解决这一问题,需要强制System.Net.FtpWebRequest命令恢复到它如何使用工作在旧的行为.Net框架2.0 / 3.5和发出的实际命令之前发出额外的CWD命令。

为了做到这一点,下面的code需要被放置在System.Net.FtpWebRequest类被调用任何实例之前。下面仅code需要被调用一次,因为它改变了整个应用程序域的设置。

 私有静态无效SetMethodRequiresCWD()
{
    键入的RequestType = typeof运算(的FtpWebRequest);
    字段信息methodInfoField = requestType.GetField(m_MethodInfo,BindingFlags.NonPublic可| BindingFlags.Instance);
    键入methodInfoType = methodInfoField.FieldType;


    字段信息knownMethodsField = methodInfoType.GetField(KnownMethodInfo,BindingFlags.Static | BindingFlags.NonPublic可);
    阵列knownMethodsArray =(阵列)knownMethodsField.GetValue(空);

    字段信息flagsField = methodInfoType.GetField(标志,BindingFlags.NonPublic可| BindingFlags.Instance);

    INT MustChangeWorkingDirectoryToPath = 0x100处;
    的foreach(在knownMethodsArray对象knownMethod)
    {
        INT标志=(int)的flagsField.GetValue(knownMethod);
        旗帜| = MustChangeWorkingDirectoryToPath;
        flagsField.SetValue(knownMethod,旗);
    }
}
 

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

I am trying to upload images to the ftp. I need to have it in a compressed folder called by a specific name and then upload that folder to a specific directory. Each time I try, I get an error The remote server returned an error: (550) File unavailable This code works fine when I am trying to upload one image at a time. Here I am trying to upload a whole folder. I checked the uri (I copied it from the debugging) and it went there just fine. Is there a different way that I have to do the upload folders? I thought that it was a write permissions issue, but I can manually login and upload a folder to the proper place. I then tried getting the directory listing which I am able to. I am not able to upload the folder to the root either. I am pretty desperate! I don't even know where to google!

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

解决方案

In order to resolve this issue, it is required to force the System.Net.FtpWebRequest command to revert back to the old behavior of how it used to work in .Net Framework 2.0/3.5 and issue the extra CWD command before issuing the actual command.

In order to do this, the following code needs to be placed before any instance of the System.Net.FtpWebRequest class is invoked. The code below only needs to be called once, since it changes the settings of the entire application domain.

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

这篇关于上传文件到FTP子返回错误(550)文件不可用,无法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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