开发者

Uploading folder to ftp subfolder returns error (550) file unavailable, no access

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


You probably need to check to see if the directory (folder) exists and if it doesn't then you need to create it on the FTP. I'm pretty sure that most FTP clients do this for you.


I had to go into NTFS permissions, and add IUSR with "full control" to the ftp folder.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜