所以我在本地有一个需要保持相同名称的文件。但是当我将文件上传到FTP时我想重命名它,或者在文件名中添加一些ekstra。这是我上传文件的方式。
string localPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string fileName = "//XmlDocument.xml";
FtpWebRequest requestFTPUploader = (FtpWebRequest)WebRequest.Create("ftp://000.000.000/Documents" + fileName);
requestFTPUploader.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
requestFTPUploader.Method = WebRequestMethods.Ftp.UploadFile;
FileInfo fileInfo = new FileInfo(localPath + fileName);
FileStream fileStream = fileInfo.OpenRead();
int bufferLength = 2048;
byte[] buffer = new byte[bufferLength];
Stream uploadStream = requestFTPUploader.GetRequestStream();
int contentLength = fileStream.Read(buffer, 0, bufferLength);
while (contentLength != 0)
{
uploadStream.Write(buffer, 0, contentLength);
contentLength = fileStream.Read(buffer, 0, bufferLength);
}
uploadStream.Close();
fileStream.Close();
requestFTPUploader = null;
答案 0 :(得分:1)
您尝试上传的文件名与您上传到的服务器上的名称之间没有任何关系。
您可以将foo.jpg
上传为bar.exe
,但没有人愿意关注。
所以改变这个:
FtpWebRequest requestFTPUploader = (FtpWebRequest)WebRequest.Create("..." + fileName);
对此:
FtpWebRequest requestFTPUploader = (FtpWebRequest)WebRequest.Create("..." + ftpFileName);
你很高兴。