使用不同的扩展名将文件上传到 SFTP 服务器
Upload file to SFTP server with different extension
我在 Stack Overflow 上浏览了很多帖子,但没有找到我需要的东西。
I have a file suppose temp.csv
我想将此文件上传到工作正常的 SFTP 服务器。
I need to save this file with different extension like temp.csv.ready
能否就此提出一些建议。
这是我试过的代码,运行良好。但我无法更改文件扩展名。
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = System.Configuration.ConfigurationManager.AppSettings["puthost"].ToString(),
UserName = System.Configuration.ConfigurationManager.AppSettings["putusername"].ToString(),
Password = System.Configuration.ConfigurationManager.AppSettings["putpassword"].ToString(),
PortNumber = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["putport"].ToString()),
SshHostKeyFingerprint = ... //followed by your 16 bit key
};
using (Session session = new Session())
{
session.SessionLogPath = "log.txt";
session.Open(sessionOptions); //Attempts to connect to your sFtp site
//Get Ftp File
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary; //The Transfer Mode -
//<em style="font-size: 9pt;">Automatic, Binary, or Ascii
transferOptions.FilePermissions = null; //Permissions applied to remote files;
//null for default permissions. Can set user,
//Group, or other Read/Write/Execute permissions.
transferOptions.PreserveTimestamp = false; //Set last write time of
//destination file to that of source file - basically change the timestamp
//to match destination and source files.
transferOptions.ResumeSupport.State = TransferResumeSupportState.Off;
WinSCP.TransferOperationResult transferResult;
//the parameter list is: local Path, Remote Path, Delete source file?, transfer Options
transferResult = session.PutFiles(@System.Configuration.ConfigurationManager.AppSettings["sendfilesource"].ToString(), System.Configuration.ConfigurationManager.AppSettings["sendfiletarget"].ToString(), false, transferOptions);
}
Session.PutFiles
method 的 remotePath
参数是:
Full path to upload the file to.
因此,您需要做的就是指定完整路径,例如:
/remote/path/temp.csv.ready
如果你愿意使用一个库,你可能想使用一个3rd library like componenentpro sftp library。您可以使用 client.UploadFile(@"xxx\temp.csv", "/temp.new.csv") 来完成这项工作。该行代码摘自以下代码示例:
// Create a new class instance.
Sftp client = new Sftp();
// Connect to the SFTP server.
client.Connect("localhost");
// Authenticate.
client.Authenticate("test", "test");
// ...
// Upload local file 'c:\test.dat' to '/test.dat'.
client.UploadFile(@"xxx\temp.csv", "/temp.new.csv");
// ...
// Disconnect.
client.Disconnect();
我在 Stack Overflow 上浏览了很多帖子,但没有找到我需要的东西。
I have a file suppose
temp.csv
我想将此文件上传到工作正常的 SFTP 服务器。
I need to save this file with different extension like
temp.csv.ready
能否就此提出一些建议。
这是我试过的代码,运行良好。但我无法更改文件扩展名。
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = System.Configuration.ConfigurationManager.AppSettings["puthost"].ToString(),
UserName = System.Configuration.ConfigurationManager.AppSettings["putusername"].ToString(),
Password = System.Configuration.ConfigurationManager.AppSettings["putpassword"].ToString(),
PortNumber = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["putport"].ToString()),
SshHostKeyFingerprint = ... //followed by your 16 bit key
};
using (Session session = new Session())
{
session.SessionLogPath = "log.txt";
session.Open(sessionOptions); //Attempts to connect to your sFtp site
//Get Ftp File
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary; //The Transfer Mode -
//<em style="font-size: 9pt;">Automatic, Binary, or Ascii
transferOptions.FilePermissions = null; //Permissions applied to remote files;
//null for default permissions. Can set user,
//Group, or other Read/Write/Execute permissions.
transferOptions.PreserveTimestamp = false; //Set last write time of
//destination file to that of source file - basically change the timestamp
//to match destination and source files.
transferOptions.ResumeSupport.State = TransferResumeSupportState.Off;
WinSCP.TransferOperationResult transferResult;
//the parameter list is: local Path, Remote Path, Delete source file?, transfer Options
transferResult = session.PutFiles(@System.Configuration.ConfigurationManager.AppSettings["sendfilesource"].ToString(), System.Configuration.ConfigurationManager.AppSettings["sendfiletarget"].ToString(), false, transferOptions);
}
Session.PutFiles
method 的 remotePath
参数是:
Full path to upload the file to.
因此,您需要做的就是指定完整路径,例如:
/remote/path/temp.csv.ready
如果你愿意使用一个库,你可能想使用一个3rd library like componenentpro sftp library。您可以使用 client.UploadFile(@"xxx\temp.csv", "/temp.new.csv") 来完成这项工作。该行代码摘自以下代码示例:
// Create a new class instance.
Sftp client = new Sftp();
// Connect to the SFTP server.
client.Connect("localhost");
// Authenticate.
client.Authenticate("test", "test");
// ...
// Upload local file 'c:\test.dat' to '/test.dat'.
client.UploadFile(@"xxx\temp.csv", "/temp.new.csv");
// ...
// Disconnect.
client.Disconnect();