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

问题描述



我可以上传,但不知道如何重新命名它。
$ 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);

我得到的错误是


解决方案

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

解决方案

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

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

06-27 18:23