繁体   English   中英

将文件上传到不同扩展名的 SFTP 服务器

[英]Upload file to SFTP server with different extension

我浏览了许多关于 Stack Overflow 的帖子,但我还没有找到我需要的东西。

我有一个文件假设temp.csv

我想将此文件上传到工作正常的 SFTP 服务器。

我需要用不同的扩展名保存这个文件,比如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方法remotePath参数是:

将文件上传到的完整路径。

因此,您需要做的就是指定完整路径,例如:

/remote/path/temp.csv.ready

如果您愿意使用库,则可能需要使用第三个库,例如 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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM