Ink*_*art 1 sftp file-upload ssh.net
我正在尝试使用 SSH.NET 将 XML 文件上传到 SFTP 服务器。(.NetCore 3.1 / VS 2019 / C#)
上传文件的远程文件夹路径是“/testin/”,我们已经确认我们有足够的写权限将文件上传到这个文件夹。我能够在此连接上连接并授权到 SFTP 服务器。但是,当我尝试上传文件时,SSH.net 仅在异常消息中返回“失败”,而没有其他明确的详细信息。我可以毫无问题地下载文件。通过检查SSH.NET“上传”功能的摘要细节如下,它指定它将返回明确的权限错误信息等。我在互联网上进行了一些搜索,但没有取得太大的成功。
// Summary:
// Uploads stream into remote file.
//
// Parameters:
// input:
// Data input stream.
//
// path:
// Remote file path.
//
// canOverride:
// if set to true then existing file will be overwritten.
//
// uploadCallback:
// The upload callback.
//
// Exceptions:
// T:System.ArgumentNullException:
// input is null.
//
// T:System.ArgumentException:
// path is null or contains only whitespace characters.
//
// T:Renci.SshNet.Common.SshConnectionException:
// Client is not connected.
//
// T:Renci.SshNet.Common.SftpPermissionDeniedException:
// Permission to upload the file was denied by the remote host.
// -or-
// A SSH command was denied by the server.
//
// T:Renci.SshNet.Common.SshException:
// A SSH error where System.Exception.Message is the message from the remote host.
//
// T:System.ObjectDisposedException:
// The method was called after the client was disposed.
//
// Remarks:
// Method calls made by this method to input, may under certain conditions result
// in exceptions thrown by the stream.
public void UploadFile(Stream input, string path, bool canOverride, Action<ulong> uploadCallback = null);
Run Code Online (Sandbox Code Playgroud)
这是我的代码:知道吗?
using (var sftp = new SftpClient(Host, TcpPort, Username, Password))
{
try
{
sftp.Connect();
var localFile = Path.Combine(LocalUploadPath + LocalFilename);
// var path = $"{localFile.Replace(@"\", "/")}";
using (var fs = File.OpenRead(localFile))
{
sftp.UploadFile(fs, RemoteUploadRootPath, true);
}
}
catch (Exception ex)
{
sftp.Disconnect();
throw new Exception($"{ex.Message} {ex.InnerException}"
}
finally
{
sftp.Disconnect();
}
}
Run Code Online (Sandbox Code Playgroud)
Ink*_*art 11
找到问题了,很简单,在上传路径中,文件路径中没有指定文件名。:| 此链接可能值得份额SSH.NET有一些很好的例子: https://csharp.hotexamples.com/examples/Renci.SshNet.Sftp/SftpUploadAsyncResult/Update/php-sftpuploadasyncresult-update-method-examples.html
这是工作正常的更新代码:
using (var sftp = new SftpClient(Host, TcpPort, Username, Password))
{
try
{
sftp.Connect();
var localFile = Path.Combine(LocalUploadPath + LocalFilename);
var remoteFilePath = Path.Combine("/testin/" + LocalFilename);
using (var fs = File.OpenRead(localFile))
{
sftp.UploadFile(fs, remoteFilePath, true);//, UploadCallBack);
}
}
catch (Exception ex)
{
sftp.Disconnect();
throw new Exception($"{ex.Message} {ex.InnerException}");
}
finally
{
sftp.Disconnect();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4734 次 |
| 最近记录: |