How to rename a file after upload

后端 未结 2 1445
一生所求
一生所求 2020-12-21 17:44

I have to upload file using Ftp protocol on server, and rename uploaded file after uploading.

I can upload it, but don\'t know how to rename it.

Code looks l

相关标签:
2条回答
  • 2020-12-21 18:21

    Why not just upload it with the correct filename in stead? Change your first line with the filename you actually want.

    FtpWebRequest requestFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServer + "/" + "httpdocs/webroot/" + destination + "/" + newFileName));
    

    But do open the reading stream from your old filename.

    0 讨论(0)
  • 2020-12-21 18:24

    RenameTo is a property, not a method. Your code should read:

    // requestFTP has been set to null in the previous line
    requestFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServer + "/" + "httpdocs/webroot/" + destination + "/" + fileName));
    requestFTP.Proxy = null;
    requestFTP.Credentials = new NetworkCredential(ftpUser, ftpPassword);
    
    string newFilename = fileName.Replace(".ftp", "");
    requestFTP.Method = WebRequestMethods.Ftp.Rename;
    requestFTP.RenameTo = newFilename;
    requestFTP.GetResponse();
    
    0 讨论(0)
提交回复
热议问题