Unable to rename file with ftp methods when current user directory is different from root

后端 未结 2 856
忘了有多久
忘了有多久 2020-12-16 20:00

Remark: due to spam prevention mechanizm I was forced to replace the beginning of the Uris from ftp:// to ftp.

I\'ve got following problem. I have to upload file wit

相关标签:
2条回答
  • 2020-12-16 20:19

    I have encountered a similar issue. The problem is that FtpWebRequest (incorrectly) prepends '/' to rename requests, as can be seen from this log (upload & rename):

    URL: 
      http://127.0.0.1/Test.txt
    FTP log:
      STOR Test.txt.part
      RNFR /Test.txt.part
      RNTO /Test.txt
    

    Please note that this problem occurs only when you are uploading to the root directory. If you changed the URL to http://127.0.0.1/path/Test.txt, then everything would work fine.

    My solution to this problem is to use %2E (dot) as the path:

    URL:
      http://127.0.0.1/%2E/Test.txt
    FTP log:
     STOR ./Test.txt.part
     RNFR ./Test.txt.part
     RNTO ./Test.txt
    

    You have to url-encode the dot, otherwise FtpWebRequest would simplify the path "/./" to "/".

    0 讨论(0)
  • 2020-12-16 20:24

    C#

    using System.Net;
    using System.IO;
    

    Rename Filename on FTP Server function

    C#

     private void RenameFileName(string currentFilename, string newFilename)
       {
           FTPSettings.IP = "DOMAIN NAME";
           FTPSettings.UserID = "USER ID";
           FTPSettings.Password = "PASSWORD";
           FtpWebRequest reqFTP = null;
           Stream ftpStream = null ;
           try
           {
    
               reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + FTPSettings.IP + "/" + currentFilename));
               reqFTP.Method = WebRequestMethods.Ftp.Rename;
               reqFTP.RenameTo = newFilename;
               reqFTP.UseBinary = true;
               reqFTP.Credentials = new NetworkCredential(FTPSettings.UserID, FTPSettings.Password);
               FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
               ftpStream = response.GetResponseStream();
               ftpStream.Close();
               response.Close();
           }
           catch (Exception ex)
           {
               if (ftpStream != null)
               {
                   ftpStream.Close();
                   ftpStream.Dispose();
               }
               throw new Exception(ex.Message.ToString());
           }
       }
    
       public static class FTPSettings
       {
           public static string IP { get; set; }
           public static string UserID { get; set; }
           public static string Password { get; set; }
       }
    
    0 讨论(0)
提交回复
热议问题