【问题标题】:Getting Error when trying to UploadFile with Stream尝试使用 Stream 上传文件时出错
【发布时间】:2022-06-23 20:41:43
【问题描述】:

我正在开发 .NET 6.0 应用程序以及 SFTP Renci.SshNet 库。我有从字符串中隐藏的数据流。我正在尝试UploadFile(Stream, filename),但出现错误。不知道我从拼图中错过了什么

error

 at Renci.SshNet.Sftp.SftpSession.RequestOpen(String path, Flags flags, Boolean nullOnError)
 at Renci.SshNet.SftpClient.InternalUploadFile(Stream input, String path, Flags flags, SftpUploadAsyncResult asyncResult, Action`1 uploadCallback)
 at Renci.SshNet.SftpClient.UploadFile(Stream input, String path, Boolean canOverride, Action`1 uploadCallback)
 at Renci.SshNet.SftpClient.UploadFile(Stream input, String path, Action`1 uploadCallback)

我在代码中给出的文件名在 SFTP 服务器上不存在,因为我希望自动创建

Upload Stream

public bool UploadStream(Tenant destinationTenant, Stream stream)
    {
        string fileStream = string.Empty;
        bool isFileUploaded = false;
        var remoteFilePath = destinationTenant.RemoteDirectoryPath;

        var sftpClient = sftpClients.FirstOrDefault(_ => _.Key == destinationTenant.TenantId.ToString()).Value;

        if (sftpClient == null)
        { Console.Write($"Unable To Retrieve SFTP Client Configuration; Dated [{DateTime.UtcNow}] "); }

        try
        {
            if (sftpClient != null)
            {
                sftpClient.Connect();

                if (sftpClient.IsConnected)
                {
                    sftpClient.ChangeDirectory(remoteFilePath);

                    sftpClient.UploadFile(stream, "/myfile.txt");
                }
            }
         }
        catch (Exception ex)
        {
            throw;
        }
        finally
        {
            if (sftpClient != null)
            { sftpClient.Disconnect(); }
        }

        return isFileUploaded;
    }
}

【问题讨论】:

    标签: c# sftp .net-6.0


    【解决方案1】:

    找到答案。应该是

    sftpClient.UploadFile(stream, "myfile.txt");
    

    【讨论】:

      猜你喜欢
      • 2017-08-27
      • 2018-04-10
      • 2013-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-02
      相关资源
      最近更新 更多