上传后如何重命名文件 [英] How to rename a file after upload

查看:125
本文介绍了上传后如何重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我可以上传,但不知道如何重新命名它。
$ b

代码如下所示:

  FtpWebRequest requestFTP =(FtpWebRequest )FtpWebRequest.Create(新的Uri(ftp://+ ftpServer +/+httpdocs / webroot /+ destination +/+ fileName)); 
requestFTP.Proxy = null;

requestFTP.Credentials = new NetworkCredential(ftpUser,ftpPassword);
requestFTP.Method = WebRequestMethods.Ftp.UploadFile;
FileStream fStream = fileInfo.OpenRead();
int bufferLength = 2048;
byte [] buffer = new byte [bufferLength];
流uploadStream = requestFTP.GetRequestStream();
int contentLength = fStream.Read(buffer,0,bufferLength);
while(contentLength!= 0)
{
uploadStream.Write(buffer,0,contentLength);
contentLength = fStream.Read(buffer,0,bufferLength);
}
uploadStream.Close();
fStream.Close();

requestFTP = null;

string newFilename = fileName.Replace(。ftp,);
requestFTP.Method = WebRequestMethods.Ftp.Rename; //这样做会造成问题
requestFTP.RenameTo(newFilename);

我得到的错误是


错误2不可使用的成员'System.Net.FtpWebRequest.RenameTo'
不能像方法一样使用。


解决方案

RenameTo是一个属性,而不是一个方法。你的代码应该是这样的:

$ $ p $ $ $ $ $ $ $ $ $ $ $ requestFTP =(FtpWebRequest) FtpWebRequest.Create(新的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();


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 like this:

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

requestFTP.Credentials = new NetworkCredential(ftpUser, ftpPassword);
requestFTP.Method = WebRequestMethods.Ftp.UploadFile;
FileStream fStream = fileInfo.OpenRead();
int bufferLength = 2048;
byte[] buffer = new byte[bufferLength];
Stream uploadStream = requestFTP.GetRequestStream();
int contentLength = fStream.Read(buffer, 0, bufferLength);
while (contentLength != 0)
{
  uploadStream.Write(buffer, 0, contentLength);
  contentLength = fStream.Read(buffer, 0, bufferLength);
}
uploadStream.Close();
fStream.Close();

requestFTP = null;

string newFilename = fileName.Replace(".ftp", "");
requestFTP.Method = WebRequestMethods.Ftp.Rename; // this like makes a problem
requestFTP.RenameTo(newFilename);

Error I'm getting is

Error 2 Non-invocable member 'System.Net.FtpWebRequest.RenameTo' cannot be used like a method.

解决方案

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();

这篇关于上传后如何重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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