File upload with WinSCP .NET/COM with temporary filenames

北慕城南 提交于 2019-12-04 07:26:57

You are right that the "transfer to temporary file name" feature of WinSCP looks like the way to go.

It makes WinSCP upload file with the .filepart appended to its name, stripping the extension once done.

TransferOptions transferOptions = new TransferOptions();
transferOptions.ResumeSupport.State = TransferResumeSupportState.On;
session.PutFiles(@"d:\toupload\myfile.dat", "/home/user/", false, transferOptions).Check();

Though it is supported with an SFTP protocol only.


With an FTP protocol, you have to do this manually.

session.PutFiles(@"d:\toupload\myfile.dat", "/home/user/myfile.dat.filepart").Check();
session.MoveFile("/home/user/myfile.dat.filepart", "/home/user/myfile.dat");

If you are uploading multiple files, you can use an operation mask, and iterate list of successfully uploaded files returned by the Session.PutFiles in TransferOperationResult, calling the Session.MoveFile for each.

TransferOperationResult transferResult;
transferResult = session.PutFiles(@"d:\toupload\*.dat", "/home/user/*.filepart")

// Throw on any error
transferResult.Check();

// Rename uploaded files
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
    string finalName = transfer.Destination.Replace(".filepart", ".dat");
    session.MoveFile(transfer.Destination, finalName);
}

There's also a complete PowerShell example in the article Locking files while uploading / Upload to temporary file name.


See also SFTP file lock mechanism (applies to FTP as well) for different approaches to hiding files being uploaded.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!