在将文件实际FTP到服务器之前,如何将文件重命名为时间戳或随机唯一编号?
例如:如果我选择C:\ taco.pdf....。1321981871.pdf实际上是将ftp'到服务器的内容。
FileInfo toUpload = new FileInfo(this.txtFile.Text);
FtpWebRequest request =
(FtpWebRequest)WebRequest.Create(
"ftp://192.168.0.186" + "/" + toUpload.Name
);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials =
new NetworkCredential("myuser","mypassword");
Stream ftpStream = request.GetRequestStream();
FileStream file = File.OpenRead(this.txtFile.Text);
int length = 1024;
byte[] buffer = new byte[length];
int bytesRead = 0;
do
{
bytesRead = file.Read(buffer, 0, length);
ftpStream.Write(buffer, 0, bytesRead);
}
while (bytesRead != 0);
file.Close();
ftpStream.Close();
最佳答案
如果您只想上传一个具有不同名称的文件而不在本地重命名,就不能更改
FtpWebRequest request =
(FtpWebRequest)WebRequest.Create(
"ftp://192.168.0.186" + "/" + toUpload.Name
);
至
FtpWebRequest request =
(FtpWebRequest)WebRequest.Create(
"ftp://192.168.0.186/whatever file name you want");
?